|
1 | 1 | package org.devinprogress.autoharvest;
|
2 | 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 net.minecraft.block.*; |
13 |
| -import net.minecraft.client.Minecraft; |
14 |
| -import net.minecraft.client.settings.KeyBinding; |
15 |
| -import net.minecraft.entity.player.EntityPlayer; |
16 |
| -import net.minecraft.init.Items; |
17 |
| -import net.minecraft.item.Item; |
18 |
| -import net.minecraft.item.ItemStack; |
| 3 | +import net.minecraft.client.resources.I18n; |
19 | 4 | import net.minecraft.util.ChatComponentText;
|
20 |
| -import net.minecraft.util.Vec3; |
21 |
| -import net.minecraft.world.World; |
22 |
| -import net.minecraftforge.common.MinecraftForge; |
23 |
| -import org.lwjgl.input.Keyboard; |
| 5 | +import net.minecraftforge.fml.client.FMLClientHandler; |
| 6 | +import net.minecraftforge.fml.common.FMLCommonHandler; |
| 7 | +import net.minecraftforge.fml.common.Mod; |
| 8 | +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; |
| 9 | +import net.minecraftforge.fml.relauncher.Side; |
| 10 | +import net.minecraftforge.fml.relauncher.SideOnly; |
24 | 11 |
|
25 |
| -import java.util.*; |
26 |
| - |
27 |
| -/** |
28 |
| - * Auto Harvest Mod |
29 |
| - * This Mod can help you harvest crops automatically. |
30 |
| - * It can also help you clear the ground. |
31 |
| - * Works for both SSP & SMP |
32 |
| - * This Mod is published under GPLv3 |
33 |
| - * Use it *AT YOUR OWN RISK* |
34 |
| - * RecursiveG |
35 |
| - * 2014 Sept. 29th |
36 |
| - * |
37 |
| - * |
38 |
| - * |
39 |
| - * Auto Harvest Mod |
40 |
| - * Copyright (C) 2014 RecursiveG |
41 |
| - * |
42 |
| - * This program is free software: you can redistribute it and/or modify |
43 |
| - * it under the terms of the GNU General Public License as published by |
44 |
| - * the Free Software Foundation, either version 3 of the License, or |
45 |
| - * (at your option) any later version. |
46 |
| - * |
47 |
| - * This program is distributed in the hope that it will be useful, |
48 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
49 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
50 |
| - * GNU General Public License for more details. |
51 |
| - * |
52 |
| - * You should have received a copy of the GNU General Public License |
53 |
| - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
54 |
| - */ |
55 |
| - |
56 |
| -@Mod(modid="autoharvest", name="Auto Harvest Mod", version="1.0") |
| 12 | +@Mod(modid = "autoharvest", name = "Auto Harvest", version = "0.2") |
| 13 | +@SideOnly(Side.CLIENT) |
57 | 14 | public class AutoHarvest {
|
58 |
| - private boolean enabled=false; |
59 |
| - private boolean harvestTick=true; |
60 |
| - private KeyBinding toggleKey=new KeyBinding("Toggle Enabled/Disabled", Keyboard.KEY_H,"Auto Harvest Mod"); |
61 |
| - private static int harvestRange=1; |
62 |
| - private Minecraft mc=null; |
| 15 | + public enum HarvestMode { |
| 16 | + //SMART, // Harvest then re-plant |
| 17 | + EAGER, // Harvest only |
| 18 | + PLANT, // Plant only |
| 19 | + SEED, // Harvest seeds & flowers |
| 20 | + FEED, // Feed animals |
| 21 | + //ATTACK, // Attack zombies |
| 22 | + OFF; // Turn off mod |
| 23 | + private static HarvestMode[] vals = values(); |
63 | 24 |
|
64 |
| - private static final Set<Integer> grassBlockIds =new HashSet<Integer>(Arrays.asList(new Integer[]{ |
65 |
| - 6,31,32,37,38,39,40,175 |
66 |
| - })); |
67 |
| - private static final Set<Item> plantableItems=new HashSet<Item>(){{ |
68 |
| - add(Items.wheat_seeds); |
69 |
| - add(Items.carrot); |
70 |
| - add(Items.potato); |
71 |
| - add(Items.melon_seeds); |
72 |
| - add(Items.pumpkin_seeds); |
73 |
| - }}; |
74 |
| - private static final Map<Class<?>,Item> harvestMap=new HashMap<Class<?>,Item>(){{ |
75 |
| - put(BlockCrops.class,Items.wheat_seeds); |
76 |
| - put(BlockCarrot.class,Items.carrot); |
77 |
| - put(BlockPotato.class,Items.potato); |
78 |
| - put(BlockNetherWart.class,Items.nether_wart); |
79 |
| - }}; |
80 |
| - static final Map<Class<?>,Integer> cropMatureData=new HashMap<Class<?>, Integer>(){{ |
81 |
| - put(BlockCrops.class,7); |
82 |
| - put(BlockCarrot.class,7); |
83 |
| - put(BlockPotato.class,7); |
84 |
| - put(BlockNetherWart.class,3); |
85 |
| - }}; |
| 25 | + public AutoHarvest.HarvestMode next() { |
| 26 | + return vals[(this.ordinal() + 1) % vals.length]; |
| 27 | + } |
| 28 | + } |
86 | 29 |
|
| 30 | + @Mod.Instance |
| 31 | + public static AutoHarvest instance; |
| 32 | + private HarvestMode mode = HarvestMode.OFF; |
| 33 | + private TickListener listener = null; |
87 | 34 |
|
88 | 35 | @Mod.EventHandler
|
89 |
| - @SuppressWarnings("unused") |
90 |
| - public void load(FMLInitializationEvent event) { |
91 |
| - ClientRegistry.registerKeyBinding(toggleKey); |
92 |
| - MinecraftForge.EVENT_BUS.register(this); |
93 |
| - FMLCommonHandler.instance().bus().register(this); |
| 36 | + public void preInit(FMLPreInitializationEvent event) { |
| 37 | + FMLCommonHandler.instance().bus().register(new KeyPressListener()); |
94 | 38 | }
|
95 | 39 |
|
96 |
| - private void sendPlayerPrivateMsg(String str){ |
97 |
| - FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText(str)); |
98 |
| - } |
99 |
| - |
100 |
| - @SubscribeEvent |
101 |
| - @SuppressWarnings("unused") |
102 |
| - public void onToggle(InputEvent.KeyInputEvent e){ |
103 |
| - if(toggleKey.isPressed()){ |
104 |
| - if(!enabled){ |
105 |
| - enabled=true; |
106 |
| - mc=FMLClientHandler.instance().getClient(); |
107 |
| - sendPlayerPrivateMsg("[Auto Harvest] Enabled"); |
108 |
| - }else{ |
109 |
| - enabled=false; |
110 |
| - sendPlayerPrivateMsg("[Auto Harvest] Disabled"); |
111 |
| - } |
| 40 | + private void setEnabled() { |
| 41 | + if (listener == null) { |
| 42 | + listener = new TickListener(mode); |
| 43 | + FMLCommonHandler.instance().bus().register(listener); |
112 | 44 | }
|
113 | 45 | }
|
114 | 46 |
|
115 |
| - @SubscribeEvent |
116 |
| - public void onInGameTick(TickEvent.PlayerTickEvent e){//one block a tick |
117 |
| - if(enabled && e.side==Side.CLIENT && e.player!=null){ |
118 |
| - if(e.player.inventory.getCurrentItem()==null){ |
119 |
| - doClearGrass(e.player); |
120 |
| - }else{ |
121 |
| - if(harvestTick) |
122 |
| - doHarvest(e.player); |
123 |
| - else |
124 |
| - doPlant(e.player); |
125 |
| - harvestTick=!harvestTick; |
126 |
| - } |
| 47 | + private void setDisabled() { |
| 48 | + if (listener != null) { |
| 49 | + listener.self_stop(); |
| 50 | + listener = null; |
127 | 51 | }
|
128 | 52 | }
|
129 | 53 |
|
130 |
| - private void doClearGrass(EntityPlayer p){ |
131 |
| - World w=p.worldObj; |
132 |
| - int X=(int)Math.floor(p.posX); |
133 |
| - int Y=(int)Math.floor(p.posY-1.45);//the "leg block" |
134 |
| - int Z=(int)Math.floor(p.posZ); |
135 |
| - for(int deltaY=-1;deltaY<=1;++deltaY) |
136 |
| - for(int deltaX=-2;deltaX<=2;++deltaX) |
137 |
| - for(int deltaZ=-2;deltaZ<=2;++deltaZ) |
138 |
| - if(grassBlockIds.contains(Block.getIdFromBlock(w.getBlock(X+deltaX,Y+deltaY,Z+deltaZ)))){ |
139 |
| - mc.playerController.onPlayerDamageBlock(X+deltaX,Y+deltaY,Z+deltaZ,1); |
140 |
| - return; |
141 |
| - } |
| 54 | + public HarvestMode toNextMode() { |
| 55 | + setDisabled(); |
| 56 | + mode = mode.next(); |
| 57 | + if (mode != HarvestMode.OFF) { |
| 58 | + setEnabled(); |
| 59 | + } |
| 60 | + return mode; |
142 | 61 | }
|
143 | 62 |
|
144 |
| - private void doHarvest(EntityPlayer p){ |
145 |
| - World w=p.worldObj; |
146 |
| - int X=(int)Math.floor(p.posX); |
147 |
| - int Y=(int)Math.floor(p.posY-1.45);//the "leg block" |
148 |
| - int Z=(int)Math.floor(p.posZ); |
149 |
| - for(int deltaX=-harvestRange;deltaX<=harvestRange;++deltaX) |
150 |
| - for(int deltaZ=-harvestRange;deltaZ<=harvestRange;++deltaZ){ |
151 |
| - if(canHarvest(w,p,X+deltaX,Y,Z+deltaZ)) { |
152 |
| - mc.playerController.onPlayerDamageBlock(X+deltaX,Y,Z+deltaZ,1); |
153 |
| - return; |
154 |
| - } |
155 |
| - } |
| 63 | + public void toNextMode(HarvestMode nextMode) { |
| 64 | + setDisabled(); |
| 65 | + mode = nextMode; |
| 66 | + if (mode != HarvestMode.OFF) { |
| 67 | + setEnabled(); |
| 68 | + } |
156 | 69 | }
|
157 | 70 |
|
158 |
| - private void doPlant(EntityPlayer p){ |
159 |
| - World w=p.worldObj; |
160 |
| - int X=(int)Math.floor(p.posX); |
161 |
| - int Y=(int)Math.floor(p.posY-2.45);//Block player stand on; |
162 |
| - int Z=(int)Math.floor(p.posZ); |
163 |
| - for(int deltaX=-harvestRange;deltaX<=harvestRange;++deltaX) |
164 |
| - for(int deltaZ=-harvestRange;deltaZ<=harvestRange;++deltaZ){ |
165 |
| - if(canPlantOn(w,p,X+deltaX,Y,Z+deltaZ)) { |
166 |
| - ItemStack seed=mc.thePlayer.inventory.getCurrentItem(); |
167 |
| - mc.playerController.onPlayerRightClick(p,w,seed,X+deltaX,Y,Z+deltaZ,1, |
168 |
| - Vec3.createVectorHelper(X+deltaX+0.5,Y+1,Z+deltaZ+0.5)); |
169 |
| - return; |
170 |
| - } |
171 |
| - } |
| 71 | + private static void sendMessage(String msg) { |
| 72 | + FMLClientHandler.instance().getClient().thePlayer.addChatMessage(new ChatComponentText(msg)); |
172 | 73 | }
|
173 | 74 |
|
174 |
| - private boolean canHarvest(World w,EntityPlayer p,int X,int Y,int Z){ |
175 |
| - Class<?> c=w.getBlock(X, Y, Z).getClass(); |
176 |
| - return harvestMap.containsKey(c) && cropMatureData.get(c)==w.getBlockMetadata(X,Y,Z) && |
177 |
| - harvestMap.get(c) == p.inventory.getCurrentItem().getItem(); |
| 75 | + public static void sendI18nMsg(String id) { |
| 76 | + sendMessage(i18n(id)); |
178 | 77 | }
|
179 | 78 |
|
180 |
| - private boolean canPlantOn(World w,EntityPlayer p,int X,int Y,int Z){ |
181 |
| - Item i=p.inventory.getCurrentItem().getItem(); |
182 |
| - return w.getBlock(X,Y+1,Z)instanceof BlockAir && |
183 |
| - (w.getBlock(X,Y,Z) instanceof BlockFarmland && plantableItems.contains(i) || |
184 |
| - w.getBlock(X,Y,Z) instanceof BlockSoulSand && i==Items.nether_wart); |
| 79 | + private static String i18n(String key) { |
| 80 | + return I18n.format(key); |
185 | 81 | }
|
186 | 82 | }
|
0 commit comments