Skip to content

Commit

Permalink
Autoswitch toggle and seed requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryhon0 committed Jun 25, 2024
1 parent 060ecea commit aa6ea05
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 49 deletions.
67 changes: 61 additions & 6 deletions src/main/java/xyz/ryhon/replanterplus/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class ConfigScreen extends Screen {

SwitchButton enabledButton;
SwitchButton sneakToggleButton;
SwitchButton missingItemNotificationsButton;
SwitchButton autoSwitchButton;
SwitchButton requireSeedHeldButton;
SimpleSlider tickDelaySlider;
ButtonWidget doneButton;

Expand All @@ -34,7 +37,7 @@ protected void init() {
int panelWidth = 256;

enabledButton = new SwitchButton(
(width / 2) + (panelWidth / 2) - (buttonWidth), (height / 2) - (buttonHeight * 2),
(width / 2) + (panelWidth / 2) - (buttonWidth), (height / 2) - (buttonHeight * 4),
buttonWidth, buttonHeight, ReplanterPlus.enabled) {
@Override
public void setToggled(boolean toggled) {
Expand Down Expand Up @@ -63,23 +66,75 @@ public void setToggled(boolean toggled) {
addSelectableChild(sneakToggleButton);
t = new TextWidget(Text.translatable("replanter.configscreen.sneakToggle"), textRenderer);
t.setPosition((width / 2) - (panelWidth / 2),
sneakToggleButton.getY() + (buttonHeight / 2) - (textRenderer.fontHeight / 2));
sneakToggleButton.getY() + (buttonHeight / 2) - (textRenderer.fontHeight / 2));
addDrawableChild(t);

missingItemNotificationsButton = new SwitchButton(
sneakToggleButton.getX(), sneakToggleButton.getY() + sneakToggleButton.getHeight(),
sneakToggleButton.getWidth(), sneakToggleButton.getHeight(),
ReplanterPlus.missingItemNotifications) {
@Override
public void setToggled(boolean toggled) {
super.setToggled(toggled);
ReplanterPlus.missingItemNotifications = toggled;
}
};
addDrawableChild(missingItemNotificationsButton);
addSelectableChild(missingItemNotificationsButton);
t = new TextWidget(Text.translatable("replanter.configscreen.missingItemNotifications"), textRenderer);
t.setPosition((width / 2) - (panelWidth / 2),
missingItemNotificationsButton.getY() + (buttonHeight / 2) - (textRenderer.fontHeight / 2));
addDrawableChild(t);

autoSwitchButton = new SwitchButton(
missingItemNotificationsButton.getX(),
missingItemNotificationsButton.getY() + missingItemNotificationsButton.getHeight(),
missingItemNotificationsButton.getWidth(), missingItemNotificationsButton.getHeight(),
ReplanterPlus.autoSwitch) {
@Override
public void setToggled(boolean toggled) {
super.setToggled(toggled);
ReplanterPlus.autoSwitch = toggled;
}
};
addDrawableChild(autoSwitchButton);
addSelectableChild(autoSwitchButton);
t = new TextWidget(Text.translatable("replanter.configscreen.autoSwitch"), textRenderer);
t.setPosition((width / 2) - (panelWidth / 2),
autoSwitchButton.getY() + (buttonHeight / 2) - (textRenderer.fontHeight / 2));
addDrawableChild(t);

requireSeedHeldButton = new SwitchButton(
autoSwitchButton.getX(),
autoSwitchButton.getY() + autoSwitchButton.getHeight(),
autoSwitchButton.getWidth(), autoSwitchButton.getHeight(),
ReplanterPlus.requireSeedHeld) {
@Override
public void setToggled(boolean toggled) {
super.setToggled(toggled);
ReplanterPlus.requireSeedHeld = toggled;
}
};
addDrawableChild(requireSeedHeldButton);
addSelectableChild(requireSeedHeldButton);
t = new TextWidget(Text.translatable("replanter.configscreen.requireSeedHeld"), textRenderer);
t.setPosition((width / 2) - (panelWidth / 2),
requireSeedHeldButton.getY() + (buttonHeight / 2) - (textRenderer.fontHeight / 2));
addDrawableChild(t);

t = new TextWidget(Text.translatable("replanter.configscreen.tickDelay"), textRenderer);
t.setPosition((width / 2) - (panelWidth / 2),
sneakToggleButton.getY() + buttonHeight);
requireSeedHeldButton.getY() + buttonHeight);
addDrawableChild(t);

tickDelaySlider = new SimpleSlider(0, 8);
tickDelaySlider.setPosition(t.getX(), t.getY() + t.getHeight());
tickDelaySlider.setWidth(panelWidth);
tickDelaySlider.setHeight(24);
tickDelaySlider.setIValue(ReplanterPlus.useDelay);
tickDelaySlider.onValue = (Long l) ->
{
tickDelaySlider.onValue = (Long l) -> {
long i = l;
ReplanterPlus.useDelay = (int)i;
ReplanterPlus.useDelay = (int) i;
};
addDrawableChild(tickDelaySlider);
addSelectableChild(tickDelaySlider);
Expand Down
117 changes: 76 additions & 41 deletions src/main/java/xyz/ryhon/replanterplus/ReplanterPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ public class ReplanterPlus implements ModInitializer {
public static Boolean enabled = true;
public static Boolean sneakToggle = true;
public static int useDelay = 4;
public static Boolean missingItemNotifications = true;
public static Boolean autoSwitch = true;
public static Boolean requireSeedHeld = false;

int ticks = 0;
final int autoSaveTicks = 20 * 60 * 3;

@Override
public void onInitialize() {
loadConfig();

String bindCategory = "category.replanter";
KeyBinding menuBind = new KeyBinding("key.replanter.menu", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN,
bindCategory);
Expand Down Expand Up @@ -95,9 +98,10 @@ public void onInitialize() {
breakAndReplantCocoa(p, state, hitResult);
return ActionResult.SUCCESS;
} else {
if (findAndEquipSeed(player, Items.BONE_MEAL)) {
Hand h = findAndEquipSeed(player, Items.BONE_MEAL);
if (h != null) {
useIgnore = true;
mc.interactionManager.interactBlock(p, Hand.OFF_HAND, hitResult);
mc.interactionManager.interactBlock(p, h, hitResult);
useIgnore = false;
return ActionResult.SUCCESS;
}
Expand All @@ -106,11 +110,14 @@ public void onInitialize() {
if (isGrown(state)) {
breakAndReplant(p, hitResult);
return ActionResult.SUCCESS;
} else if (findAndEquipSeed(player, Items.BONE_MEAL)) {
useIgnore = true;
mc.interactionManager.interactBlock(p, Hand.OFF_HAND, hitResult);
useIgnore = false;
return ActionResult.SUCCESS;
} else {
Hand h = findAndEquipSeed(player, Items.BONE_MEAL);
if (h != null) {
useIgnore = true;
mc.interactionManager.interactBlock(p, h, hitResult);
useIgnore = false;
return ActionResult.SUCCESS;
}
}
}

Expand All @@ -122,6 +129,9 @@ Boolean findInstamineTool(ClientPlayerEntity p, BlockState state, BlockPos pos)
if (state.calcBlockBreakingDelta(p, p.getWorld(), pos) >= 1f)
return true;

if (!autoSwitch)
return false;

int currentSlot = p.getInventory().selectedSlot;
for (int i = 0; i < PlayerInventory.getHotbarSize(); i++) {
p.getInventory().selectedSlot = i;
Expand Down Expand Up @@ -166,34 +176,38 @@ else if (block instanceof PitcherCropBlock pcb)
}

void breakAndReplant(ClientPlayerEntity player, BlockHitResult hit) {
useIgnore = true;

Item seed = getSeed(player.getWorld().getBlockState(hit.getBlockPos()).getBlock());
Hand h = findAndEquipSeed(player, seed);
if (requireSeedHeld && h == null) {
sendMissingItemMessage(player, seed);
return;
}

holdFortuneItem(player);
mc.interactionManager.attackBlock(hit.getBlockPos(), hit.getSide());

if (findAndEquipSeed(player, seed)) {
if (h != null) {
useIgnore = true;
mc.interactionManager.interactBlock(player, Hand.OFF_HAND, hit.withBlockPos(
mc.interactionManager.interactBlock(player, h, hit.withBlockPos(
hit.getBlockPos()));
useIgnore = false;
mc.itemUseCooldown = useDelay;
} else {
player.sendMessage(
Text.translatable(seed.getTranslationKey())
.append(Text.translatable("replanter.gui.seed_not_found"))
.setStyle(Style.EMPTY.withColor(0xFF0000)),
true);
}

useIgnore = false;
} else
sendMissingItemMessage(player, seed);
mc.itemUseCooldown = useDelay;
}

void breakAndReplantCocoa(ClientPlayerEntity p, BlockState state, BlockHitResult hitResult) {
if (findInstamineTool(p, state, hitResult.getBlockPos())) {
Item seed = state.getBlock().asItem();
Hand h = findAndEquipSeed(p, seed);

if (requireSeedHeld && h == null) {
sendMissingItemMessage(p, seed);
return;
}

mc.interactionManager.attackBlock(hitResult.getBlockPos(), hitResult.getSide());
if (findAndEquipSeed(p, state.getBlock().asItem())) {
if (h != null) {
Direction dir = (Direction) state.get(CocoaBlock.FACING);

float x, y, z;
Expand All @@ -205,16 +219,11 @@ void breakAndReplantCocoa(ClientPlayerEntity p, BlockState state, BlockHitResult
hitResult.getBlockPos().add(dir.getVector()));

useIgnore = true;
mc.interactionManager.interactBlock(p, Hand.OFF_HAND, placeHit);
mc.interactionManager.interactBlock(p, h, placeHit);
useIgnore = false;
mc.itemUseCooldown = useDelay;
} else {
p.sendMessage(
Text.translatable(state.getBlock().asItem().getTranslationKey())
.append(Text.translatable("replanter.gui.seed_not_found"))
.setStyle(Style.EMPTY.withColor(0xFF0000)),
true);
}
} else
sendMissingItemMessage(p, seed);
mc.itemUseCooldown = useDelay;
}
}

Expand All @@ -232,34 +241,42 @@ Item getSeed(Block block) {
return null;
}

boolean findAndEquipSeed(PlayerEntity p, Item item) {
Hand findAndEquipSeed(PlayerEntity p, Item item) {
if (item == null)
return false;
return null;

PlayerInventory pi = p.getInventory();
if (pi.getStack(pi.selectedSlot).isOf(item))
return Hand.MAIN_HAND;
if (pi.getStack(PlayerInventory.OFF_HAND_SLOT).isOf(item))
return true;
return Hand.OFF_HAND;

if (!autoSwitch)
return null;

for (int i = 0; i < PlayerInventory.getHotbarSize(); i++) {
if (pi.getStack(i).isOf(item)) {
pi.selectedSlot = i;
mc.interactionManager.syncSelectedSlot();
mc.getNetworkHandler().sendPacket(new PlayerActionC2SPacket(
PlayerActionC2SPacket.Action.SWAP_ITEM_WITH_OFFHAND, BlockPos.ORIGIN, Direction.DOWN));
return true;
return Hand.OFF_HAND;
}
}
return false;
return null;
}

void holdFortuneItem(PlayerEntity p) {
int maxLevel = 0;
int slot = -1;

PlayerInventory pi = p.getInventory();
Optional<RegistryEntry.Reference<Enchantment>> fortune = p.getWorld().getRegistryManager().get(RegistryKeys.ENCHANTMENT).getEntry(Enchantments.FORTUNE);
Optional<RegistryEntry.Reference<Enchantment>> fortune = p.getWorld().getRegistryManager()
.get(RegistryKeys.ENCHANTMENT).getEntry(Enchantments.FORTUNE);
// Server removed the Fortune enchantment????
if(!fortune.isPresent()) return;

if (!fortune.isPresent())
return;

for (int i = 0; i < PlayerInventory.getHotbarSize(); i++) {
int lvl = EnchantmentHelper.getLevel(fortune.get(), pi.getStack(i));
if (lvl > maxLevel) {
Expand All @@ -274,6 +291,16 @@ void holdFortuneItem(PlayerEntity p) {
}
}

void sendMissingItemMessage(PlayerEntity player, Item seed) {
if (missingItemNotifications)
player.sendMessage(
Text.translatable(seed.getTranslationKey())
.append(Text.translatable(
autoSwitch ? "replanter.gui.seed_not_in_hotbar" : "replanter.gui.seed_not_in_hand"))
.setStyle(Style.EMPTY.withColor(0xFF0000)),
true);
}

static Path configDir = FabricLoader.getInstance().getConfigDir().resolve("replanterplus");
static Path configFile = configDir.resolve("config.json");

Expand All @@ -292,7 +319,12 @@ static void loadConfig() {
sneakToggle = jo.get("sneakToggle").getAsBoolean();
if (jo.has("useDelay"))
useDelay = jo.get("useDelay").getAsInt();

if (jo.has("missingItemNotifications"))
missingItemNotifications = jo.get("missingItemNotifications").getAsBoolean();
if (jo.has("autoSwitch"))
autoSwitch = jo.get("autoSwitch").getAsBoolean();
if (jo.has("requireSeedHeld"))
requireSeedHeld = jo.get("requireSeedHeld").getAsBoolean();
} catch (Exception e) {
LOGGER.error("Failed to load config", e);
}
Expand All @@ -304,6 +336,9 @@ static void saveConfig() {
jo.add("enabled", new JsonPrimitive(enabled));
jo.add("sneakToggle", new JsonPrimitive(sneakToggle));
jo.add("useDelay", new JsonPrimitive(useDelay));
jo.add("missingItemNotifications", new JsonPrimitive(missingItemNotifications));
jo.add("autoSwitch", new JsonPrimitive(autoSwitch));
jo.add("requireSeedHeld", new JsonPrimitive(requireSeedHeld));

try {
Files.createDirectories(configDir);
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/assets/replanter-plus/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
"category.replanter": "Replanter Plus",
"key.replanter.menu": "Open Config Scren",
"key.replanter.toggle": "Toggle Replanter",
"replanter.gui.seed_not_found": ": item not in hotbar",
"replanter.gui.seed_not_in_hotbar": " not in hotbar",
"replanter.gui.seed_not_in_hand": " not in hand",
"replanter.configscreen.enabled": "Enabled",
"replanter.configscreen.sneakToggle": "Disable When Sneaking",
"replanter.configscreen.tickDelay": "Item Use Delay",
"replanter.configscreen.missingItemNotifications": "Missing Item Notifications",
"replanter.configscreen.autoSwitch": "Autoswitch",
"replanter.configscreen.requireSeedHeld": "Require Seed to be Held",
"replanter.configscreen.done": "Done",
"replanter.switchbutton.label.on": "on",
"replanter.switchbutton.label.off": "off"
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/assets/replanter-plus/lang/pl_pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"category.replanter": "Replanter Plus",
"key.replanter.menu": "Otwórz ekran konfiguracji",
"key.replanter.toggle": "Przełącz Replanter Plus",
"replanter.gui.seed_not_found": ": przedmiot nie w hotbarze",
"replanter.gui.seed_not_in_hotbar": " nie jest w hotbarze",
"replanter.gui.seed_not_in_hand": " nie jest w dłoni",
"replanter.configscreen.enabled": "Włączony",
"replanter.configscreen.sneakToggle": "Wyłącz podczas skradania",
"replanter.configscreen.tickDelay": "Opóźnienie użycia",
"replanter.configscreen.missingItemNotifications": "Powiadomienia o braku nasion",
"replanter.configscreen.autoSwitch": "Automatyczna zmiana przedmiotów",
"replanter.configscreen.requireSeedHeld": "Wymagaj nasion w dłoni",
"replanter.configscreen.done": "Gotowe",
"replanter.switchbutton.label.on": "wł.",
"replanter.switchbutton.label.off": "wył."
Expand Down

0 comments on commit aa6ea05

Please sign in to comment.