|
| 1 | +package com.github.gtexpert.gtbm.integration.forestry.farming; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | + |
| 5 | +import javax.annotation.Nullable; |
| 6 | + |
| 7 | +import net.minecraft.block.Block; |
| 8 | +import net.minecraft.block.state.IBlockState; |
| 9 | +import net.minecraft.entity.player.EntityPlayer; |
| 10 | +import net.minecraft.init.Blocks; |
| 11 | +import net.minecraft.item.ItemStack; |
| 12 | +import net.minecraft.util.EnumActionResult; |
| 13 | +import net.minecraft.util.EnumFacing; |
| 14 | +import net.minecraft.util.EnumHand; |
| 15 | +import net.minecraft.util.math.BlockPos; |
| 16 | +import net.minecraft.world.World; |
| 17 | + |
| 18 | +import forestry.api.farming.ICrop; |
| 19 | +import forestry.api.farming.IFarmable; |
| 20 | +import forestry.api.farming.IFarmableInfo; |
| 21 | +import forestry.core.network.packets.PacketFXSignal; |
| 22 | +import forestry.core.utils.NetworkUtil; |
| 23 | +import forestry.farming.logic.crops.CropDestroy; |
| 24 | + |
| 25 | +public class FarmableGTCEuSapling implements IFarmable { |
| 26 | + |
| 27 | + private final Block saplingBlock; |
| 28 | + private final ItemStack germling; |
| 29 | + private final ItemStack[] windfall; |
| 30 | + |
| 31 | + public FarmableGTCEuSapling(Block saplingBlock, ItemStack[] windfall) { |
| 32 | + this.saplingBlock = saplingBlock; |
| 33 | + this.germling = new ItemStack(saplingBlock); |
| 34 | + this.windfall = windfall; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public boolean isSaplingAt(World world, BlockPos pos, IBlockState blockState) { |
| 39 | + return blockState.getBlock() == saplingBlock; |
| 40 | + } |
| 41 | + |
| 42 | + @Nullable |
| 43 | + @Override |
| 44 | + public ICrop getCropAt(World world, BlockPos pos, IBlockState blockState) { |
| 45 | + Block block = blockState.getBlock(); |
| 46 | + if (!block.isWood(world, pos)) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + return new CropDestroy(world, blockState, pos); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean isGermling(ItemStack itemstack) { |
| 54 | + return ItemStack.areItemsEqual(germling, itemstack); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void addInformation(IFarmableInfo info) { |
| 59 | + info.addGermlings(Collections.singletonList(germling)); |
| 60 | + info.addProducts(windfall); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean isWindfall(ItemStack itemstack) { |
| 65 | + for (ItemStack drop : windfall) { |
| 66 | + if (drop.isItemEqual(itemstack)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean plantSaplingAt(EntityPlayer player, ItemStack germling, World world, BlockPos pos) { |
| 75 | + ItemStack originalHeldItem = player.getHeldItem(EnumHand.MAIN_HAND); |
| 76 | + try { |
| 77 | + ItemStack copy = germling.copy(); |
| 78 | + player.setHeldItem(EnumHand.MAIN_HAND, copy); |
| 79 | + EnumActionResult result = copy.onItemUse(player, world, pos.down(), |
| 80 | + EnumHand.MAIN_HAND, EnumFacing.UP, 0, 0, 0); |
| 81 | + if (result == EnumActionResult.SUCCESS) { |
| 82 | + PacketFXSignal packet = new PacketFXSignal( |
| 83 | + PacketFXSignal.SoundFXType.BLOCK_PLACE, pos, |
| 84 | + Blocks.SAPLING.getDefaultState()); |
| 85 | + NetworkUtil.sendNetworkPacket(packet, pos, world); |
| 86 | + return true; |
| 87 | + } |
| 88 | + return false; |
| 89 | + } finally { |
| 90 | + player.setHeldItem(EnumHand.MAIN_HAND, originalHeldItem); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments