Skip to content
This repository was archived by the owner on Jan 3, 2020. It is now read-only.

Commit d9970c7

Browse files
committed
cocoa & auto refill
1 parent 9e9e2ca commit d9970c7

File tree

3 files changed

+152
-18
lines changed

3 files changed

+152
-18
lines changed

src/main/java/org/devinprogress/autoharvest/AutoHarvest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.devinprogress.autoharvest;
22

3+
import net.minecraft.client.entity.EntityPlayerSP;
34
import net.minecraft.client.resources.I18n;
5+
import net.minecraft.inventory.ClickType;
6+
import net.minecraft.item.ItemStack;
7+
import net.minecraft.network.play.client.CPacketClickWindow;
48
import net.minecraft.util.text.TextComponentString;
59
import net.minecraftforge.common.MinecraftForge;
610
import net.minecraftforge.fml.client.FMLClientHandler;
@@ -73,4 +77,18 @@ public static void msg(String key, Object... obj) {
7377
+ I18n.format(key, obj)
7478
));
7579
}
80+
81+
public static void moveInventoryItem(int srcIdx, int dstIdx) {
82+
EntityPlayerSP p = FMLClientHandler.instance().getClientPlayerEntity();
83+
ItemStack[] a = p.inventory.mainInventory;
84+
if (a[srcIdx] != null) {
85+
p.sendQueue.addToSendQueue(new CPacketClickWindow(0, srcIdx < 9 ? srcIdx + 36 : srcIdx, 0, ClickType.PICKUP, a[srcIdx], (short) 0));
86+
p.sendQueue.addToSendQueue(new CPacketClickWindow(0, dstIdx < 9 ? dstIdx + 36 : dstIdx, 0, ClickType.PICKUP, a[dstIdx], (short) 1));
87+
p.sendQueue.addToSendQueue(new CPacketClickWindow(0, srcIdx < 9 ? srcIdx + 36 : srcIdx, 0, ClickType.PICKUP, a[srcIdx], (short) 2));
88+
// return;
89+
ItemStack tmp = a[srcIdx];
90+
a[srcIdx] = a[dstIdx];
91+
a[dstIdx] = tmp;
92+
}
93+
}
7694
}

src/main/java/org/devinprogress/autoharvest/CropManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.google.common.collect.Multimap;
77
import net.minecraft.block.Block;
88
import net.minecraft.block.BlockCrops;
9+
import net.minecraft.block.BlockOldLog;
10+
import net.minecraft.block.BlockPlanks;
911
import net.minecraft.block.state.IBlockState;
1012
import net.minecraft.entity.passive.*;
1113
import net.minecraft.init.Blocks;
@@ -110,4 +112,8 @@ public static boolean canPlantOn(Item m, World w, BlockPos p) {
110112
if (!SEED_MAP.containsValue(m)) return false;
111113
return SEED_MAP.inverse().get(m).canPlaceBlockAt(w, p);
112114
}
115+
116+
public static boolean isJungleLog(IBlockState s) {
117+
return s.getBlock() == Blocks.LOG && s.getValue(BlockOldLog.VARIANT) == BlockPlanks.EnumType.JUNGLE;
118+
}
113119
}

src/main/java/org/devinprogress/autoharvest/TickListener.java

Lines changed: 128 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,58 @@ private void harvestTick() {
8585
int Z = (int) Math.floor(p.posZ);
8686
for (int deltaX = -range; deltaX <= range; ++deltaX)
8787
for (int deltaZ = -range; deltaZ <= range; ++deltaZ) {
88-
BlockPos pos = new BlockPos(X + deltaX, Y, Z + deltaZ);
89-
IBlockState state = w.getBlockState(pos);
90-
Block b = state.getBlock();
91-
if (CropManager.isCropMature(w, pos, state, b)) {
92-
Minecraft.getMinecraft().playerController.onPlayerDamageBlock(pos, EnumFacing.UP);
93-
return;
88+
for (int deltaY = -1; deltaY <= 1; ++deltaY) {
89+
BlockPos pos = new BlockPos(X + deltaX, Y + deltaY, Z + deltaZ);
90+
IBlockState state = w.getBlockState(pos);
91+
Block b = state.getBlock();
92+
if (CropManager.isCropMature(w, pos, state, b)) {
93+
Minecraft.getMinecraft().playerController.onPlayerDamageBlock(pos, EnumFacing.UP);
94+
return;
95+
}
9496
}
9597
}
9698
}
9799

100+
private void minusOneInHand() {
101+
ItemStack st = p.getHeldItem(EnumHand.MAIN_HAND);
102+
if (st != null) {
103+
if (st.stackSize <= 1) {
104+
p.setHeldItem(EnumHand.MAIN_HAND, null);
105+
} else {
106+
st.stackSize--;
107+
}
108+
}
109+
}
110+
98111
private ItemStack lastUsedItem = null;
99112

100113
private ItemStack tryFillItemInHand() {
101114
ItemStack itemStack = p.getHeldItem(EnumHand.MAIN_HAND);
102115
if (itemStack == null) {
103-
// TODO
104-
AutoHarvest.msg("notify.lack_of_seed");
105-
AutoHarvest.msg("notify.switch_to.off");
106-
AutoHarvest.instance.toNextMode(AutoHarvest.HarvestMode.OFF);
107-
lastUsedItem = null;
108-
return null;
116+
int supplmentIdx = -1;
117+
ItemStack stack = null;
118+
if (lastUsedItem != null) {
119+
ItemStack[] inv = p.inventory.mainInventory;
120+
for (int idx = 0; idx < 36; ++idx) {
121+
if (inv[idx] != null && inv[idx].getItem() == lastUsedItem.getItem() &&
122+
inv[idx].getItemDamage() == lastUsedItem.getItemDamage() &&
123+
inv[idx].hasTagCompound() == false) {
124+
supplmentIdx = idx;
125+
stack = inv[idx];
126+
break;
127+
}
128+
}
129+
}
130+
if (supplmentIdx < 0) {
131+
AutoHarvest.msg("notify.lack_of_seed");
132+
AutoHarvest.msg("notify.switch_to.off");
133+
AutoHarvest.instance.toNextMode(AutoHarvest.HarvestMode.OFF);
134+
lastUsedItem = null;
135+
return null;
136+
}
137+
AutoHarvest.moveInventoryItem(supplmentIdx, p.inventory.currentItem);
138+
return stack;
109139
} else {
110-
lastUsedItem = null;
111140
return itemStack;
112141
}
113142
}
@@ -117,12 +146,10 @@ private void plantTick() {
117146
if (handItem == null) return;
118147
if (!CropManager.isSeed(handItem)) {
119148
if (CropManager.isCocoa(handItem)) {
120-
lastUsedItem = handItem;
121-
plantCocoaTick();
149+
plantCocoaTick(handItem);
122150
}
123151
return;
124152
}
125-
lastUsedItem = handItem;
126153

127154
World w = p.worldObj;
128155
int X = (int) Math.floor(p.posX);
@@ -141,12 +168,94 @@ private void plantTick() {
141168
handItem, downPos, EnumFacing.UP,
142169
new Vec3d(X + deltaX + 0.5, Y, Z + deltaZ + 0.5),
143170
EnumHand.MAIN_HAND);
171+
lastUsedItem = handItem;
172+
minusOneInHand();
144173
}
145174
}
146175
}
147176

148-
private void plantCocoaTick() {
149-
// TODO
177+
private void plantCocoaTick(ItemStack handItem) {
178+
World w = p.worldObj;
179+
int X = (int) Math.floor(p.posX);
180+
int Y = (int) Math.floor(p.posY + 0.2D);//the "leg block" , in case in soul sand
181+
int Z = (int) Math.floor(p.posZ);
182+
183+
for (int deltaX = -range; deltaX <= range; ++deltaX) {
184+
for (int deltaZ = -range; deltaZ <= range; ++deltaZ) {
185+
for (int deltaY = 0; deltaY <= 7; ++deltaY) {
186+
BlockPos pos = new BlockPos(X + deltaX, Y + deltaY, Z + deltaZ);
187+
if (!canReachBlock(p, pos)) continue;
188+
IBlockState jungleBlock = w.getBlockState(pos);
189+
if (CropManager.isJungleLog(jungleBlock)) {
190+
BlockPos tmpPos;
191+
192+
EnumFacing tmpFace = EnumFacing.EAST;
193+
tmpPos = pos.add(tmpFace.getDirectionVec());
194+
if (w.getBlockState(tmpPos).getBlock() == Blocks.AIR) {
195+
FMLClientHandler.instance().getClient().playerController.processRightClickBlock(
196+
p,
197+
FMLClientHandler.instance().getWorldClient(),
198+
handItem, pos, tmpFace,
199+
new Vec3d(X + deltaX + 1, Y + deltaY + 0.5, Z + deltaZ + 0.5),
200+
EnumHand.MAIN_HAND);
201+
lastUsedItem = handItem;
202+
minusOneInHand();
203+
return;
204+
}
205+
206+
tmpFace = EnumFacing.WEST;
207+
tmpPos = pos.add(tmpFace.getDirectionVec());
208+
if (w.getBlockState(tmpPos).getBlock() == Blocks.AIR) {
209+
FMLClientHandler.instance().getClient().playerController.processRightClickBlock(
210+
p,
211+
FMLClientHandler.instance().getWorldClient(),
212+
handItem, pos, tmpFace,
213+
new Vec3d(X + deltaX, Y + deltaY + 0.5, Z + deltaZ + 0.5),
214+
EnumHand.MAIN_HAND);
215+
lastUsedItem = handItem;
216+
minusOneInHand();
217+
return;
218+
}
219+
220+
tmpFace = EnumFacing.SOUTH;
221+
tmpPos = pos.add(tmpFace.getDirectionVec());
222+
if (w.getBlockState(tmpPos).getBlock() == Blocks.AIR) {
223+
FMLClientHandler.instance().getClient().playerController.processRightClickBlock(
224+
p,
225+
FMLClientHandler.instance().getWorldClient(),
226+
handItem, pos, tmpFace,
227+
new Vec3d(X + deltaX + 0.5, Y + deltaY + 0.5, Z + deltaZ + 1),
228+
EnumHand.MAIN_HAND);
229+
lastUsedItem = handItem;
230+
minusOneInHand();
231+
return;
232+
}
233+
234+
tmpFace = EnumFacing.NORTH;
235+
tmpPos = pos.add(tmpFace.getDirectionVec());
236+
if (w.getBlockState(tmpPos).getBlock() == Blocks.AIR) {
237+
FMLClientHandler.instance().getClient().playerController.processRightClickBlock(
238+
p,
239+
FMLClientHandler.instance().getWorldClient(),
240+
handItem, pos, tmpFace,
241+
new Vec3d(X + deltaX + 0.5, Y + deltaY + 0.5, Z + deltaZ),
242+
EnumHand.MAIN_HAND);
243+
lastUsedItem = handItem;
244+
minusOneInHand();
245+
return;
246+
}
247+
}
248+
}
249+
}
250+
}
251+
}
252+
253+
private boolean canReachBlock(EntityPlayerSP playerEntity, BlockPos blockpos) {
254+
double d0 = playerEntity.posX - ((double) blockpos.getX() + 0.5D);
255+
double d1 = playerEntity.posY - ((double) blockpos.getY() + 0.5D) + 1.5D;
256+
double d2 = playerEntity.posZ - ((double) blockpos.getZ() + 0.5D);
257+
double d3 = d0 * d0 + d1 * d1 + d2 * d2;
258+
return d3 <= 36D;
150259
}
151260

152261
private void feedTick() {
@@ -161,6 +270,7 @@ private void feedTick() {
161270
FMLClientHandler.instance().getClient().playerController
162271
.interactWithEntity(p, e, handItem, EnumHand.MAIN_HAND);
163272
lastUsedItem = handItem;
273+
minusOneInHand();
164274
return;
165275
}
166276
}

0 commit comments

Comments
 (0)