Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Harvest handling for blocks with a material that does not require a t…
Browse files Browse the repository at this point in the history
…ool (aka enforcing an axe for planks/logs)
  • Loading branch information
bonii-xx committed Oct 3, 2016
1 parent fc172a2 commit cd2ae3a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
package iguanaman.iguanatweakstconstruct.override;
package slimeknights.harvesttweaks;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

// This class handles harvest levels on the same block but where some metadata requires a tool,
// and some metadatas don't
public class ExtraHarvestLevelHandler {

@SubscribeEvent
public void breakSpeed(PlayerEvent.BreakSpeed event)
{
if(event.entityPlayer == null)
public void breakSpeed(PlayerEvent.BreakSpeed event) {
EntityPlayer player = event.getEntityPlayer();
if(player == null) {
return;
}

if(event.block == null || event.block == Blocks.air)
if(event.getState() == null || event.getState().getBlock().isAir(event.getState(), null, null)) {
return;
}

Block block = event.block;
int hlvl = event.block.getHarvestLevel(event.metadata);
IBlockState state = event.getState();
Block block = state.getBlock();
int hlvl = block.getHarvestLevel(state);

// does the block require a tool?
if(hlvl <= 0)
if(hlvl < 0) {
// no, nothing to do
return;
}

// tool requires a harvest level, but does the material require a tool?
if(!block.getMaterial().isToolNotRequired())
// block requires a harvest level, but does the material require a tool?
if(!state.getMaterial().isToolNotRequired()) {
// a tool is required, we don't have to do anything
return;
}

// The tool does NOT require a tool, but has a harvest level for a tool set
// we now manually check if this requiremnet is fulfilled

String tool = event.block.getHarvestTool(event.metadata);
ItemStack itemStack = event.entityPlayer.getCurrentEquippedItem();
String tool = block.getHarvestTool(state);
ItemStack itemStack = player.getHeldItemMainhand();

if(itemStack != null && itemStack.getItem() != null) {
if(itemStack.getItem().getHarvestLevel(itemStack, tool) >= hlvl)
// everything ok, correct tool
if(itemStack.getItem().getHarvestLevel(itemStack, tool, player, state) >= hlvl)
// everything ok, correct tool
{
return;
}
}

// we require a tool, but no fitting tool is present. we prevent any breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void postInit(FMLPostInitializationEvent event) {
CONFIG.save();

MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.register(new ExtraHarvestLevelHandler());
if(event.getSide().isServer()) {
MinecraftForge.EVENT_BUS.register(ConfigSyncHandler.INSTANCE);
}
Expand Down

0 comments on commit cd2ae3a

Please sign in to comment.