Skip to content

Commit

Permalink
Add Bonemealing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
flier268 committed Jul 20, 2020
1 parent 21d71fe commit d00d56d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/flier268/autoharvest/AutoHarvest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum HarvestMode {
PLANT, // Plant only
Farmer, //Harvest then re-plant
SEED, // Harvest seeds & flowers
BONEMEALING,
FEED, // Feed animals
FISHING;// Fishing
private static HarvestMode[] vals = values();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/flier268/autoharvest/CropManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public static boolean isCropMature(World w, BlockPos pos, BlockState stat, Block
return false;
}

public static boolean isBoneMeal(ItemStack stack) {
return (!stack.isEmpty()
&& stack.getItem() == Items.BONE_MEAL);
}

public static boolean isSeed(ItemStack stack) {
return (!stack.isEmpty()
&& SEED_MAP.containsValue(stack.getItem()));
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/flier268/autoharvest/KeyPressListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class KeyPressListener {

private KeyBinding key_Switch, key_ModeChange, key_Config;
private KeyBinding key_HARVEST, key_PLANT, key_Farmer, key_SEED, key_FEED, key_FISHING;
private KeyBinding key_HARVEST, key_PLANT, key_Farmer, key_SEED, key_FEED, key_FISHING,Key_BONEMEALING;

public KeyPressListener() {
String categoryGeneral = new TranslatableText("key.category.general").getString();
Expand Down Expand Up @@ -54,6 +54,10 @@ public KeyPressListener() {
GLFW.GLFW_KEY_UNKNOWN,
categorySwitchTo
);
Key_BONEMEALING = new KeyBinding("bonemealing",
GLFW.GLFW_KEY_UNKNOWN,
categorySwitchTo
);
KeyBindingHelper.registerKeyBinding(key_ModeChange);
KeyBindingHelper.registerKeyBinding(key_Switch);
KeyBindingHelper.registerKeyBinding(key_Config);
Expand All @@ -63,7 +67,7 @@ public KeyPressListener() {
KeyBindingHelper.registerKeyBinding(key_SEED);
KeyBindingHelper.registerKeyBinding(key_FEED);
KeyBindingHelper.registerKeyBinding(key_FISHING);

KeyBindingHelper.registerKeyBinding(Key_BONEMEALING);

ClientTickEvents.END_CLIENT_TICK.register(client -> {
onProcessKey();
Expand Down Expand Up @@ -92,6 +96,8 @@ public void onProcessKey() {
modeName = AutoHarvest.instance.toSpecifiedMode(AutoHarvest.HarvestMode.FEED).toString().toLowerCase();
} else if (key_FISHING.wasPressed()) {
modeName = AutoHarvest.instance.toSpecifiedMode(AutoHarvest.HarvestMode.FISHING).toString().toLowerCase();
} else if (Key_BONEMEALING.wasPressed()) {
modeName = AutoHarvest.instance.toSpecifiedMode(AutoHarvest.HarvestMode.BONEMEALING).toString().toLowerCase();
}
if (modeName != null)
AutoHarvest.msg("notify.switch_to", new TranslatableText(modeName).getString());
Expand Down
44 changes: 40 additions & 4 deletions src/main/java/com/flier268/autoharvest/TickListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.CropBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.passive.AnimalEntity;
Expand Down Expand Up @@ -77,6 +78,9 @@ public void onTick(ClientPlayerEntity player) {
case FISHING:
fishingTick();
break;
case BONEMEALING:
bonemealingTick();
break;
}
} catch (Exception ex) {
AutoHarvest.msg("notify.tick_error");
Expand Down Expand Up @@ -118,9 +122,9 @@ private void harvestTick() {
if (CropManager.isCropMature(w, pos, state, b)) {
if (b == Blocks.SWEET_BERRY_BUSH) {
BlockPos downPos = pos.down();
BlockHitResult blockHitResult = new BlockHitResult(new Vec3d(X + deltaX + 0.5, Y , Z + deltaZ + 0.5), Direction.UP, downPos, false);
ActionResult a= MinecraftClient.getInstance().interactionManager.interactBlock(p, MinecraftClient.getInstance().world, Hand.MAIN_HAND, blockHitResult);
String ass="";
BlockHitResult blockHitResult = new BlockHitResult(new Vec3d(X + deltaX + 0.5, Y, Z + deltaZ + 0.5), Direction.UP, downPos, false);
ActionResult a = MinecraftClient.getInstance().interactionManager.interactBlock(p, MinecraftClient.getInstance().world, Hand.MAIN_HAND, blockHitResult);
String ass = "";
} else
MinecraftClient.getInstance().interactionManager.attackBlock(pos, Direction.UP);
return;
Expand Down Expand Up @@ -289,7 +293,7 @@ private void feedTick() {
}
}
if (handItem.getItem() == Items.SHEARS) {
for (SheepEntity e : p.getEntityWorld().getEntities(SheepEntity.class, box,null)) {
for (SheepEntity e : p.getEntityWorld().getEntities(SheepEntity.class, box, null)) {
if (!e.isBaby() && !e.isSheared()) {
lastUsedItem = handItem.copy();
MinecraftClient.getInstance().interactionManager.interactEntity(p, e, Hand.MAIN_HAND);
Expand Down Expand Up @@ -341,4 +345,36 @@ private void fishingTick() {
}
}
}

/* clear all grass on land */
private void bonemealingTick() {
ItemStack handItem = p.getMainHandStack();
if (handItem == null || !CropManager.isBoneMeal(handItem)) {
return;
} else {
handItem = tryFillItemInHand();
}

World w = p.getEntityWorld();
int X = (int) Math.floor(p.getX());
int Y = (int) Math.floor(p.getY());//the "leg block"
int Z = (int) Math.floor(p.getZ());
for (int deltaY = 3; deltaY >= -2; --deltaY)
for (int deltaX = -range; deltaX <= range; ++deltaX)
for (int deltaZ = -range; deltaZ <= range; ++deltaZ) {
BlockPos pos = new BlockPos(X + deltaX, Y + deltaY, Z + deltaZ);
BlockState blockState = w.getBlockState(pos);
Block block = blockState.getBlock();
if (block instanceof CropBlock) {
//not Mature
if (!((CropBlock) block).isMature(blockState)) {
BlockHitResult blockHitResult = new BlockHitResult(new Vec3d(X + deltaX + 0.5, Y, Z + deltaZ + 0.5), Direction.UP, pos, false);
lastUsedItem = handItem.copy();
ActionResult report = MinecraftClient.getInstance().interactionManager.interactBlock(MinecraftClient.getInstance().player, MinecraftClient.getInstance().world, Hand.MAIN_HAND, blockHitResult);
minusOneInHand();
return;
}
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/autoharvest/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"seed": "Seed mode",
"feed": "Feed mode",
"fishing": "Fishing mode",
"bonemealing": "bonemealing mode",
"notify.switch_to":"Switch to: %s",
"notify.lack_of_seed":"You need more seeds",
"notify.prefix":"[AutoHarvest]",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/autoharvest/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"seed": "除草 模式",
"feed": "喂食 模式",
"fishing": "钓鱼 模式",
"bonemealing": "催熟 模式",
"notify.switch_to":"已切换到 %s",
"notify.lack_of_seed":"缺少种子",
"notify.prefix":"[自动收割]",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/autoharvest/lang/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"seed": "除草 模式",
"feed": "餵食 模式",
"fishing": "釣魚 模式",
"bonemealing": "催熟 模式",
"notify.switch_to":"已切換到 %s",
"notify.lack_of_seed":"缺少種子",
"notify.prefix":"[自動收割]",
Expand Down

0 comments on commit d00d56d

Please sign in to comment.