benchristel / refactoring-workshop

Levels up refactoring skills, in three engaging hours
6 stars 5 forks source link

Idea: type=="foo" and type=="bar" #3

Open professor opened 5 years ago

professor commented 5 years ago

There's an object or strategies waiting in every code base?

professor commented 5 years ago

Minecraft inner workings Article: https://www.minecraft.net/en-us/article/programmers-play-minecrafts-inner-workings Code: https://github.com/Mojang/brigadier

Minecraft mod tools: https://gaming.stackexchange.com/questions/52441/is-there-a-way-to-see-minecrafts-source-code http://www.modcoderpack.com/

benchristel commented 5 years ago

@professor something like this?

class Player
  def mine(tool, block)
    if Block.INDESTRUCTIBLE.include?(block.type)
      return
    end

    if tool.type == :pick && !Block.MINEABLE_BY_PICK.include?(block.type)
      # destroy the block without dropping anything
      block.destroy!
      return
    end

    if tool.type == :shovel && !Block.MINEABLE_BY_SHOVEL.include?(block.type)
      # destroy the block without dropping anything
      block.destroy!
      return
    end

    if tool.strength < Tool.STONE && block.hardness >= Block.IRON ||
       tool.strength < Tool.DIAMOND && block.hardness >= Block.OBSIDIAN
      # destroy block without dropping anything
      block.destroy!
      return
    end

    if block.type == :redstone
      # redstone drops multiple items
      block.drop_all([:redstone] * rand(4..6))
      block.destroy!
      return
    end

    if block.type == :lapis
      # lapis drops multiple items
      block.drop_all([:lapis] * rand(2..4))
      block.destroy!
      return
    end

    if block.type == :cracked_stone
      block.destroy!
      # cracked stone doesn't drop anything
      return
    end

    # default case: drop one item
    block.drop(block.type)
    block.destroy!
  end
end