Add shear mode to chainsaw, closes #1668

This commit is contained in:
modmuss50 2019-03-06 15:32:55 +00:00
parent 73465f4b38
commit 46b1e920e9

View file

@ -25,22 +25,33 @@
package techreborn.items.tools;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import reborncore.common.util.ChatUtils;
import reborncore.common.util.ItemUtils;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
import techreborn.lib.MessageIDs;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
@ -73,6 +84,18 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
if(ItemUtils.isActive(stack) && !worldIn.isRemote){
IBlockState state = worldIn.getBlockState(pos);
if(state.getBlock() instanceof IShearable){
if(((IShearable) state.getBlock()).isShearable(stack, worldIn, pos)){
List<ItemStack> results = ((IShearable) state.getBlock()).onSheared(stack, worldIn, pos, 0);
results.forEach(itemStack -> InventoryHelper.spawnItemStack(worldIn, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), itemStack));
return super.onBlockDestroyed(stack, worldIn, blockIn, pos, entityLiving);
}
}
}
List<BlockPos> wood = new ArrayList<>();
findWood(worldIn, pos, wood, new ArrayList<>());
wood.forEach(pos1 -> breakBlock(pos1, stack, worldIn, entityLiving, pos));
@ -104,6 +127,66 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
}
}
@Override
public ActionResult<ItemStack> onItemRightClick(final World world, final EntityPlayer player, final EnumHand hand) {
final ItemStack stack = player.getHeldItem(hand);
if (player.isSneaking()) {
if (new ForgePowerItemManager(stack).getEnergyStored() < cost) {
ChatUtils.sendNoSpamMessages(MessageIDs.nanosaberID, new TextComponentString(
TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberEnergyErrorTo") + " "
+ TextFormatting.GOLD + I18n
.format("techreborn.message.nanosaberActivate")));
} else {
if (!ItemUtils.isActive(stack)) {
if (stack.getTagCompound() == null) {
stack.setTagCompound(new NBTTagCompound());
}
stack.getTagCompound().setBoolean("isActive", true);
if (world.isRemote) {
ChatUtils.sendNoSpamMessages(MessageIDs.nanosaberID, new TextComponentString(
TextFormatting.GRAY + I18n.format("techreborn.message.setTo") + " "
+ TextFormatting.GOLD + I18n
.format("techreborn.message.nanosaberActive")));
}
} else {
stack.getTagCompound().setBoolean("isActive", false);
if (world.isRemote) {
ChatUtils.sendNoSpamMessages(MessageIDs.nanosaberID, new TextComponentString(
TextFormatting.GRAY + I18n.format("techreborn.message.setTo") + " "
+ TextFormatting.GOLD + I18n
.format("techreborn.message.nanosaberInactive")));
}
}
}
return new ActionResult<>(EnumActionResult.SUCCESS, stack);
}
return new ActionResult<>(EnumActionResult.PASS, stack);
}
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
if (ItemUtils.isActive(stack) && new ForgePowerItemManager(stack).getEnergyStored() < cost) {
if(worldIn.isRemote){
ChatUtils.sendNoSpamMessages(MessageIDs.nanosaberID, new TextComponentString(
TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberEnergyError") + " "
+ TextFormatting.GOLD + I18n
.format("techreborn.message.nanosaberDeactivating")));
}
stack.getTagCompound().setBoolean("isActive", false);
}
}
@SideOnly(Side.CLIENT)
@Override
public void addInformation(ItemStack stack, @Nullable
World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
if (!ItemUtils.isActive(stack)) {
tooltip.add(TextFormatting.YELLOW + "Shear: " + TextFormatting.RED + I18n.format("techreborn.message.nanosaberInactive"));
} else {
tooltip.add(TextFormatting.YELLOW + "Shear: " + TextFormatting.GREEN + I18n.format("techreborn.message.nanosaberActive"));
}
}
@Override
public boolean canHarvestBlock(IBlockState blockIn) {
return Items.DIAMOND_AXE.canHarvestBlock(blockIn);