|
| 1 | +package org.devinprogress.autoharvest; |
| 2 | + |
| 3 | +import cpw.mods.fml.client.FMLClientHandler; |
| 4 | +import cpw.mods.fml.client.registry.ClientRegistry; |
| 5 | +import cpw.mods.fml.common.FMLCommonHandler; |
| 6 | +import cpw.mods.fml.common.Mod; |
| 7 | +import cpw.mods.fml.common.event.FMLInitializationEvent; |
| 8 | +import cpw.mods.fml.common.eventhandler.SubscribeEvent; |
| 9 | +import cpw.mods.fml.common.gameevent.InputEvent; |
| 10 | +import cpw.mods.fml.common.gameevent.TickEvent; |
| 11 | +import cpw.mods.fml.relauncher.Side; |
| 12 | +import ibxm.Player; |
| 13 | +import net.minecraft.block.*; |
| 14 | +import net.minecraft.block.material.Material; |
| 15 | +import net.minecraft.client.settings.KeyBinding; |
| 16 | +import net.minecraft.entity.player.EntityPlayer; |
| 17 | +import net.minecraft.item.ItemSeeds; |
| 18 | +import net.minecraft.item.ItemStack; |
| 19 | +import net.minecraft.util.ChatComponentText; |
| 20 | +import net.minecraft.util.ChunkCoordinates; |
| 21 | +import net.minecraft.util.Vec3; |
| 22 | +import net.minecraft.world.World; |
| 23 | +import net.minecraftforge.common.MinecraftForge; |
| 24 | +import net.minecraftforge.event.entity.player.PlayerEvent; |
| 25 | +import org.lwjgl.input.Keyboard; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by recursiveg on 14-9-28. |
| 29 | + */ |
| 30 | + |
| 31 | +@Mod(modid="autoharvest", name="Auto Harvest", version="0.1-dev") |
| 32 | +public class AutoHarvest { |
| 33 | + private boolean enabled=false; |
| 34 | + private boolean harvestTick=true; |
| 35 | + private KeyBinding toggleKey=new KeyBinding("Toggle Status", Keyboard.KEY_H,"Auto Harvest Mod"); |
| 36 | + private static int[] delta={-1,0,1}; |
| 37 | + |
| 38 | + @Mod.EventHandler |
| 39 | + public void load(FMLInitializationEvent event) { |
| 40 | + ClientRegistry.registerKeyBinding(toggleKey); |
| 41 | + MinecraftForge.EVENT_BUS.register(this); |
| 42 | + FMLCommonHandler.instance().bus().register(this); |
| 43 | + } |
| 44 | + |
| 45 | + @SubscribeEvent |
| 46 | + public void onToggle(InputEvent.KeyInputEvent e){ |
| 47 | + if(toggleKey.isPressed()){ |
| 48 | + //off->harvest->plant |
| 49 | + /*if(!enabled){ |
| 50 | + enabled=true; |
| 51 | + harvestTick=true; |
| 52 | + FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText("[Auto Harvest] Current Mode: Harvest")); |
| 53 | + }else if(enabled&&harvestTick){ |
| 54 | + harvestTick=false; |
| 55 | + FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText("[Auto Harvest] Current Mode: Plant")); |
| 56 | + }else{ |
| 57 | + enabled=false; |
| 58 | + FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText("[Auto Harvest] Current Mode: OFF")); |
| 59 | + }*/ |
| 60 | + if(!enabled){ |
| 61 | + enabled=true; |
| 62 | + FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText("[Auto Harvest] Current Mode: ON")); |
| 63 | + }else{ |
| 64 | + enabled=false; |
| 65 | + FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText("[Auto Harvest] Current Mode: OFF")); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @SubscribeEvent |
| 71 | + public void onTick(TickEvent.PlayerTickEvent e){ |
| 72 | + if(enabled && e.side==Side.CLIENT && e.player!=null){ |
| 73 | + harvestTick=!harvestTick; |
| 74 | + if(harvestTick) |
| 75 | + doHarvest(e.player); |
| 76 | + else |
| 77 | + doPlant(e.player); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void doHarvest(EntityPlayer p){ |
| 82 | + World w=p.worldObj; |
| 83 | + int X=(int)Math.floor(p.posX); |
| 84 | + int Y=(int)Math.floor(p.posY-1.61); |
| 85 | + int Z=(int)Math.floor(p.posZ); |
| 86 | + for(int deltaX:delta) |
| 87 | + for(int deltaZ:delta){ |
| 88 | + if(canHarvest(w,X+deltaX,Y,Z+deltaZ)) { |
| 89 | + FMLClientHandler.instance().getClient().playerController.onPlayerDamageBlock(X + deltaX, Y, Z + deltaZ, 1); |
| 90 | + return; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private boolean canHarvest(World w,int X,int Y,int Z){ |
| 96 | + return (w.getBlock(X,Y,Z)instanceof BlockCrops&&w.getBlockMetadata(X,Y,Z)==7); |
| 97 | + } |
| 98 | + |
| 99 | + private void doPlant(EntityPlayer p){ |
| 100 | + World w=p.worldObj; |
| 101 | + int X=(int)Math.floor(p.posX); |
| 102 | + int Y=(int)Math.floor(p.posY-1.61); |
| 103 | + int Z=(int)Math.floor(p.posZ); |
| 104 | + |
| 105 | + Y=Y-1; |
| 106 | + for(int deltaX:delta) |
| 107 | + for(int deltaZ:delta){ |
| 108 | + if(canPlantOn(w, X + deltaX, Y, Z + deltaZ)) { |
| 109 | + ItemStack seed=selectSeed(p); |
| 110 | + if(seed!=null) |
| 111 | + FMLClientHandler.instance().getClient().playerController.onPlayerRightClick(p,w,seed,X + deltaX, Y, Z + deltaZ,1, |
| 112 | + Vec3.createVectorHelper(X + deltaX+0.5, Y+1, Z + deltaZ+0.5)); |
| 113 | + else{ |
| 114 | + //System.out.println("seed==null"); |
| 115 | + } |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private boolean canPlantOn(World w,int X,int Y,int Z){ |
| 122 | + return (w.getBlock(X,Y,Z) instanceof BlockFarmland)&&(w.getBlock(X,Y+1,Z)instanceof BlockAir); |
| 123 | + } |
| 124 | + |
| 125 | + private ItemStack selectSeed(EntityPlayer p){ |
| 126 | + if(p==null||p.inventory==null||p.inventory.getCurrentItem()==null)return null; |
| 127 | + if(p.inventory.getCurrentItem().getItem() instanceof ItemSeeds) |
| 128 | + return p.inventory.getCurrentItem(); |
| 129 | + else |
| 130 | + return null; |
| 131 | + } |
| 132 | +} |
0 commit comments