diff --git a/build.gradle b/build.gradle index d137d44..c3b6bb5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'org.ajoberstar.grgit' version '5.0.0' - id 'fabric-loom' version '1.0-SNAPSHOT' + id 'fabric-loom' version '1.4-SNAPSHOT' } sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17 diff --git a/gradle.properties b/gradle.properties index 1073da3..121eac7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,13 +1,13 @@ org.gradle.jvmargs=-Xmx4G # Fabric (https://fabricmc.net/versions.html) -minecraft_version=1.20.1 -yarn_mappings=1.20.1+build.2 -loader_version=0.14.21 +minecraft_version=1.20.2 +yarn_mappings=1.20.2+build.4 +loader_version=0.14.23 # Mod Properties -mod_version=1.4.2 +mod_version=1.4.3 maven_group=maxsuperman.addons archives_base_name=villager-roller -meteor_version=0.5.4-SNAPSHOT +meteor_version=0.5.5-SNAPSHOT diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8049c68..e411586 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/maxsuperman/addons/roller/VillagerRollerAddon.java b/src/main/java/maxsuperman/addons/roller/VillagerRollerAddon.java index 7256e8e..34f205d 100644 --- a/src/main/java/maxsuperman/addons/roller/VillagerRollerAddon.java +++ b/src/main/java/maxsuperman/addons/roller/VillagerRollerAddon.java @@ -2,27 +2,24 @@ import maxsuperman.addons.roller.modules.VillagerRoller; import meteordevelopment.meteorclient.addons.MeteorAddon; -import meteordevelopment.meteorclient.MeteorClient; import meteordevelopment.meteorclient.systems.modules.Modules; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.lang.invoke.MethodHandles; public class VillagerRollerAddon extends MeteorAddon { - public static final Logger LOG = LogManager.getLogger(); + public static final Logger LOG = LogManager.getLogger(); + + @Override + public void onInitialize() { + LOG.info("Initializing Meteor Villager Roller"); - @Override - public void onInitialize() { - LOG.info("Initializing Meteor Villager Roller"); + // Modules + Modules.get().add(new VillagerRoller()); + } - // Modules - Modules.get().add(new VillagerRoller()); - } - @Override public void onRegisterCategories() { - //Modules.registerCategory(CATEGORY); + // Custom category not defined } @Override diff --git a/src/main/java/maxsuperman/addons/roller/mixins/VillagerInteractMixin.java b/src/main/java/maxsuperman/addons/roller/mixins/VillagerInteractMixin.java index d87e7cb..26bae2b 100644 --- a/src/main/java/maxsuperman/addons/roller/mixins/VillagerInteractMixin.java +++ b/src/main/java/maxsuperman/addons/roller/mixins/VillagerInteractMixin.java @@ -14,8 +14,8 @@ class VillagerInteractMixin { @Inject(at = @At("HEAD"), method = "interactMob(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/ActionResult;", cancellable = true) public void interactMob(CallbackInfoReturnable cir) { VillagerRoller roller = Modules.get().get(VillagerRoller.class); - if (VillagerRoller.currentState == VillagerRoller.State.WaitingForTargetVillager) { - VillagerRoller.currentState = VillagerRoller.State.RollingBreakingBlock; + if (roller.currentState == VillagerRoller.State.WaitingForTargetVillager) { + roller.currentState = VillagerRoller.State.RollingBreakingBlock; roller.rollingVillager = (VillagerEntity) (Object) this; roller.info("We got your villager"); cir.setReturnValue(ActionResult.CONSUME); diff --git a/src/main/java/maxsuperman/addons/roller/modules/VillagerRoller.java b/src/main/java/maxsuperman/addons/roller/modules/VillagerRoller.java index 20418bd..12e82bc 100644 --- a/src/main/java/maxsuperman/addons/roller/modules/VillagerRoller.java +++ b/src/main/java/maxsuperman/addons/roller/modules/VillagerRoller.java @@ -31,6 +31,7 @@ import net.minecraft.block.Blocks; import net.minecraft.client.sound.PositionedSoundInstance; import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.item.ItemStack; @@ -41,6 +42,7 @@ import net.minecraft.nbt.NbtList; import net.minecraft.registry.Registries; import net.minecraft.sound.SoundEvent; +import net.minecraft.sound.SoundEvents; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -51,11 +53,9 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; - -import static net.minecraft.enchantment.EnchantmentHelper.getIdFromNbt; -import static net.minecraft.enchantment.EnchantmentHelper.getLevelFromNbt; -import static net.minecraft.sound.SoundEvents.BLOCK_AMETHYST_CLUSTER_BREAK; +import java.util.stream.Stream; public class VillagerRoller extends Module { private final SettingGroup sgGeneral = settings.getDefaultGroup(); @@ -65,25 +65,29 @@ public class VillagerRoller extends Module { .name("disable-when-found") .description("Disable enchantment from list if found") .defaultValue(true) - .build()); + .build() + ); private final Setting saveListToConfig = sgGeneral.add(new BoolSetting.Builder() .name("save-list-to-config") .description("Toggles saving and loading of rolling list to config and copypaste buffer") .defaultValue(true) - .build()); + .build() + ); private final Setting enablePlaySound = sgGeneral.add(new BoolSetting.Builder() .name("enable-sound") .description("Plays sound when it finds desired trade") .defaultValue(true) - .build()); + .build() + ); private final Setting> sound = sgSound.add(new SoundEventListSetting.Builder() .name("sound-to-play") .description("Sound that will be played when desired trade is found if enabled") - .defaultValue(Collections.singletonList(BLOCK_AMETHYST_CLUSTER_BREAK)) - .build()); + .defaultValue(Collections.singletonList(SoundEvents.BLOCK_AMETHYST_CLUSTER_BREAK)) + .build() + ); private final Setting soundPitch = sgSound.add(new DoubleSetting.Builder() .name("sound-pitch") @@ -91,7 +95,8 @@ public class VillagerRoller extends Module { .defaultValue(1.0) .min(0) .sliderRange(0, 8) - .build()); + .build() + ); private final Setting soundVolume = sgSound.add(new DoubleSetting.Builder() .name("sound-volume") @@ -99,19 +104,22 @@ public class VillagerRoller extends Module { .defaultValue(1.0) .min(0) .sliderRange(0, 1) - .build()); + .build() + ); private final Setting pauseOnScreen = sgGeneral.add(new BoolSetting.Builder() .name("pause-on-screens") .description("Pauses rolling if any screen is open") .defaultValue(true) - .build()); + .build() + ); private final Setting headRotateOnPlace = sgGeneral.add(new BoolSetting.Builder() .name("rotate-place") .description("Look to the block while placing it?") .defaultValue(true) - .build()); + .build() + ); private final Setting failedToPlaceDelay = sgGeneral.add(new IntSetting.Builder() .name("place-fail-delay") @@ -119,13 +127,15 @@ public class VillagerRoller extends Module { .defaultValue(1500) .min(0) .sliderRange(0, 10000) - .build()); + .build() + ); private final Setting failedToPlaceDisable = sgGeneral.add(new BoolSetting.Builder() .name("place-fail-disable") .description("Disables roller if block placement fails") .defaultValue(false) - .build()); + .build() + ); private final Setting maxProfessionWaitTime = sgGeneral.add(new IntSetting.Builder() .name("max-profession-wait-time") @@ -133,13 +143,15 @@ public class VillagerRoller extends Module { .defaultValue(0) .min(0) .sliderRange(0, 10000) - .build()); + .build() + ); private final Setting sortEnchantments = sgGeneral.add(new BoolSetting.Builder() .name("sort-enchantments") .description("Show enchantments sorted by their name") .defaultValue(true) - .build()); + .build() + ); public enum State { Disabled, @@ -152,12 +164,11 @@ public enum State { RollingWaitingForVillagerTrades } - public static State currentState = State.Disabled; - - public BlockPos rollingBlockPos; - public Block rollingBlock; + public State currentState = State.Disabled; public VillagerEntity rollingVillager; - public List searchingEnchants = new ArrayList<>(); + private BlockPos rollingBlockPos; + private Block rollingBlock; + private final List searchingEnchants = new ArrayList<>(); private long failedToPlacePrevMsg = System.currentTimeMillis(); private long currentProfessionWaitTime; @@ -187,7 +198,7 @@ public NbtCompound toTag() { NbtCompound tag = super.toTag(); if (saveListToConfig.get()) { NbtList l = new NbtList(); - for (rollingEnchantment e : searchingEnchants) { + for (RollingEnchantment e : searchingEnchants) { l.add(e.toTag()); } tag.put("rolling", l); @@ -206,7 +217,7 @@ public Module fromTag(NbtCompound tag) { info("Invalid list element"); continue; } - searchingEnchants.add(new rollingEnchantment().fromTag((NbtCompound) e)); + searchingEnchants.add(new RollingEnchantment().fromTag((NbtCompound) e)); } } return this; @@ -234,23 +245,21 @@ public boolean loadSearchingFromFile(File f) { info("Invalid list element"); return false; } - searchingEnchants.add(new rollingEnchantment().fromTag((NbtCompound) e)); + searchingEnchants.add(new RollingEnchantment().fromTag((NbtCompound) e)); } return true; } public boolean saveSearchingToFile(File f) { NbtList l = new NbtList(); - for (rollingEnchantment e : searchingEnchants) { + for (RollingEnchantment e : searchingEnchants) { l.add(e.toTag()); } NbtCompound c = new NbtCompound(); c.put("rolling", l); - if (Files.notExists(f.getParentFile().toPath())) { - if (!f.getParentFile().mkdirs()) { - info("Failed to make directories"); - return false; - } + if (Files.notExists(f.getParentFile().toPath()) && !f.getParentFile().mkdirs()) { + info("Failed to make directories"); + return false; } try { NbtIo.write(c, f); @@ -276,7 +285,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WTextBox nfname = control.add(theme.textBox("default")).expandWidgetX().expandCellX().expandX().widget(); WButton save = control.add(theme.button("Save")).expandX().widget(); save.action = () -> { - if (saveSearchingToFile(new File(new File(MeteorClient.FOLDER, "VillagerRoller"), nfname.get()+".nbt"))) { + if (saveSearchingToFile(new File(new File(MeteorClient.FOLDER, "VillagerRoller"), nfname.get() + ".nbt"))) { info("Saved successfully"); } else { info("Save failed"); @@ -287,27 +296,26 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { control.row(); ArrayList fnames = new ArrayList<>(); - var path = MeteorClient.FOLDER.toPath().resolve("VillagerRoller"); - if(Files.notExists(path)) { - if(!path.toFile().mkdirs()) { + Path path = MeteorClient.FOLDER.toPath().resolve("VillagerRoller"); + if (Files.notExists(path)) { + if (!path.toFile().mkdirs()) { error("Failed to create directory [{}]", path); } } else { - try (var l = Files.list(path)) { + try (Stream l = Files.list(path)) { l.forEach(p -> { String name = p.getFileName().toString(); fnames.add(name.substring(0, name.length() - 4)); }); - } catch(IOException e) { + } catch (IOException e) { error("Failed to list directory", e); } } - if (fnames.size() != 0) { - WDropdown lfname = control.add(theme.dropdown(fnames.toArray(new String[0]), "default")) - .expandWidgetX().expandCellX().expandX().widget(); + if (!fnames.isEmpty()) { + WDropdown lfname = control.add(theme.dropdown(fnames.toArray(new String[0]), "default")).expandWidgetX().expandCellX().expandX().widget(); WButton load = control.add(theme.button("Load")).expandX().widget(); load.action = () -> { - if (loadSearchingFromFile(new File(new File(MeteorClient.FOLDER, "VillagerRoller"), lfname.get()+".nbt"))) { + if (loadSearchingFromFile(new File(new File(MeteorClient.FOLDER, "VillagerRoller"), lfname.get() + ".nbt"))) { list.clear(); fillWidget(theme, list); info("Loaded successfully"); @@ -333,11 +341,11 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { } for (int i = 0; i < searchingEnchants.size(); i++) { - rollingEnchantment e = searchingEnchants.get(i); + RollingEnchantment e = searchingEnchants.get(i); final int si = i; ItemStack book = Items.ENCHANTED_BOOK.getDefaultStack(); int maxlevel = 255; - if(e.isVanilla()) { + if (e.isVanilla()) { Enchantment ench = Registries.ENCHANTMENT.get(e.enchantment); maxlevel = e.getMaxLevel(); book.addEnchantment(ench, e.minLevel == 0 ? maxlevel : e.minLevel); @@ -346,33 +354,32 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WHorizontalList label = theme.horizontalList(); WButton c = label.add(theme.button("Change")).widget(); - c.action = () -> mc.setScreen(new EnchantmentSelectScreen(theme, (rollingEnchantment sel) -> { + c.action = () -> mc.setScreen(new EnchantmentSelectScreen(theme, (RollingEnchantment sel) -> { searchingEnchants.set(si, sel); list.clear(); fillWidget(theme, list); })); - if(e.isCustom()) { + if (e.isCustom()) { label.add(theme.label(e.enchantment.toString())); } else { Registries.ENCHANTMENT.getOrEmpty(e.enchantment).ifPresent(en -> label.add(theme.label(Names.get(en)))); } table.add(label); - WIntEdit lev = table.add(theme.intEdit(e.minLevel, 0, maxlevel, true)).minWidth(40) - .expandX().widget(); + WIntEdit lev = table.add(theme.intEdit(e.minLevel, 0, maxlevel, true)).minWidth(40).expandX().widget(); lev.action = () -> e.minLevel = lev.get(); lev.tooltip = "Minimum enchantment level, 0 acts as maximum possible only (for custom 0 acts like 1)"; - var costbox = table.add(theme.horizontalList()).minWidth(50).expandX().widget(); + WHorizontalList costbox = table.add(theme.horizontalList()).minWidth(50).expandX().widget(); WIntEdit cost = costbox.add(theme.intEdit(e.maxCost, 0, 64, false)).minWidth(40).expandX().widget(); cost.action = () -> e.maxCost = cost.get(); cost.tooltip = "Maximum cost in emeralds, 0 means no limit"; - var setOptimal = costbox.add(theme.button("O")).widget(); + WButton setOptimal = costbox.add(theme.button("O")).widget(); setOptimal.tooltip = "Set to optimal price (5 + maxLevel*3) (double if treasure) (if known)"; setOptimal.action = () -> { list.clear(); - if(e.isVanilla()) { + if (e.isVanilla()) { e.maxCost = getMinimumPrice(Registries.ENCHANTMENT.get(e.enchantment), e.getMaxLevel()); } fillWidget(theme, list); @@ -401,8 +408,8 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { }; WButton create = controls.add(theme.button("Add")).expandX().widget(); - create.action = () -> mc.setScreen(new EnchantmentSelectScreen(theme, (e) -> { - if(e.isVanilla()) { + create.action = () -> mc.setScreen(new EnchantmentSelectScreen(theme, e -> { + if (e.isVanilla()) { e.minLevel = e.getMaxLevel(); e.maxCost = getMinimumPrice(Registries.ENCHANTMENT.get(e.enchantment), e.minLevel); } @@ -417,7 +424,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { list.clear(); searchingEnchants.clear(); for (Enchantment e : Registries.ENCHANTMENT.stream().filter(Enchantment::isAvailableForEnchantedBookOffer).toList()) { - searchingEnchants.add(new rollingEnchantment(Registries.ENCHANTMENT.getId(e), e.getMaxLevel(), getMinimumPrice(e, e.getMaxLevel()), false)); + searchingEnchants.add(new RollingEnchantment(Registries.ENCHANTMENT.getId(e), e.getMaxLevel(), getMinimumPrice(e, e.getMaxLevel()), false)); } fillWidget(theme, list); }; @@ -426,7 +433,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WButton setOptimalForAll = controls.add(theme.button("Set optimal for all")).expandX().widget(); setOptimalForAll.action = () -> { list.clear(); - for (rollingEnchantment e : searchingEnchants) { + for (RollingEnchantment e : searchingEnchants) { if (e.isVanilla()) { Enchantment ench = Registries.ENCHANTMENT.get(e.enchantment); e.maxCost = getMinimumPrice(ench, e.getMaxLevel()); @@ -438,10 +445,8 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WButton priceBumpUp = controls.add(theme.button("+1 to price for all")).expandX().widget(); priceBumpUp.action = () -> { list.clear(); - for (rollingEnchantment e : searchingEnchants) { - if(e.maxCost < 64) { - e.maxCost++; - } + for (RollingEnchantment e : searchingEnchants) { + if (e.maxCost < 64) e.maxCost++; } fillWidget(theme, list); }; @@ -449,10 +454,8 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WButton priceBumpDown = controls.add(theme.button("-1 to price for all")).expandX().widget(); priceBumpDown.action = () -> { list.clear(); - for (rollingEnchantment e : searchingEnchants) { - if(e.maxCost > 0) { - e.maxCost--; - } + for (RollingEnchantment e : searchingEnchants) { + if (e.maxCost > 0) e.maxCost--; } fillWidget(theme, list); }; @@ -461,7 +464,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WButton setZeroForAll = controls.add(theme.button("Set zero price for all")).expandX().widget(); setZeroForAll.action = () -> { list.clear(); - for (rollingEnchantment e : searchingEnchants) { + for (RollingEnchantment e : searchingEnchants) { e.maxCost = 0; } fillWidget(theme, list); @@ -470,7 +473,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WButton enableAll = controls.add(theme.button("Enable all")).expandX().widget(); enableAll.action = () -> { list.clear(); - for (rollingEnchantment e : searchingEnchants) { + for (RollingEnchantment e : searchingEnchants) { e.enabled = true; } fillWidget(theme, list); @@ -479,7 +482,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { WButton disableAll = controls.add(theme.button("Disable all")).expandX().widget(); disableAll.action = () -> { list.clear(); - for (rollingEnchantment e : searchingEnchants) { + for (RollingEnchantment e : searchingEnchants) { e.enabled = false; } fillWidget(theme, list); @@ -491,14 +494,12 @@ private void fillWidget(GuiTheme theme, WVerticalList list) { public static int getMinimumPrice(Enchantment e, int l) { // TradeOffers.EnchantBookFactory.create() // Lnet/minecraft/village/TradeOffers$EnchantBookFactory;create(Lnet/minecraft/entity/Entity;Lnet/minecraft/util/math/random/Random;)Lnet/minecraft/village/TradeOffer; - if(e == null) { - return 0; - } - return e.isTreasure() ? (2 + 3*l)*2 : 2 + 3*l; + if (e == null) return 0; + return e.isTreasure() ? (2 + 3 * l) * 2 : 2 + 3 * l; } public interface EnchantmentSelectCallback { - void Selection(rollingEnchantment e); + void selection(RollingEnchantment e); } public static class EnchantmentSelectScreen extends WindowScreen { @@ -507,10 +508,10 @@ public static class EnchantmentSelectScreen extends WindowScreen { private final EnchantmentSelectCallback callback; private String filterText = ""; - public EnchantmentSelectScreen(GuiTheme theme1, EnchantmentSelectCallback callback1) { - super(theme1, "Select enchantment"); - this.theme = theme1; - this.callback = callback1; + public EnchantmentSelectScreen(GuiTheme theme, EnchantmentSelectCallback callback) { + super(theme, "Select enchantment"); + this.theme = theme; + this.callback = callback; } @Override @@ -532,14 +533,10 @@ public void initWidgets() { WButton ca = customList.add(theme.button("Select")).widget(); ca.action = () -> { String idtext = cc.get(); - if(idtext.length() == 0) { - return; - } + if (idtext.isEmpty()) return; Identifier id = Identifier.tryParse(cc.get()); - if(id == null) { - return; - } - callback.Selection(new rollingEnchantment(id, 0, 0, true)); + if (id == null) return; + callback.selection(new RollingEnchantment(id, 0, 0, true)); close(); }; @@ -549,13 +546,13 @@ public void initWidgets() { private void fillTable(WTable table) { for (Enchantment e : available.stream().sorted((o1, o2) -> Names.get(o1).compareToIgnoreCase(Names.get(o2))).toList()) { - if (!filterText.equals("") && !Names.get(e).toLowerCase().startsWith(filterText.toLowerCase())) { + if (!filterText.isEmpty() && !Names.get(e).toLowerCase().startsWith(filterText.toLowerCase())) { continue; } table.add(theme.label(Names.get(e))).expandCellX(); WButton a = table.add(theme.button("Select")).widget(); a.action = () -> { - callback.Selection(new rollingEnchantment(Registries.ENCHANTMENT.getId(e), e.getMaxLevel(), getMinimumPrice(e, e.getMaxLevel()), true)); + callback.selection(new RollingEnchantment(Registries.ENCHANTMENT.getId(e), e.getMaxLevel(), getMinimumPrice(e, e.getMaxLevel()), true)); close(); }; table.row(); @@ -567,41 +564,34 @@ public void triggerInteract() { if (pauseOnScreen.get() && mc.currentScreen != null) { info("Rolling paused, interact with villager to continue"); } else { - assert mc.interactionManager != null; mc.interactionManager.interactEntity(mc.player, rollingVillager, Hand.MAIN_HAND); } } public Map getEnchants(ItemStack stack) { Map ret = Maps.newLinkedHashMap(); - if(!stack.isOf(Items.ENCHANTED_BOOK) || stack.getNbt() == null) { + if (!stack.isOf(Items.ENCHANTED_BOOK) || stack.getNbt() == null) { return ret; } NbtList list = stack.getNbt().getList("StoredEnchantments", NbtElement.COMPOUND_TYPE); - for(int i = 0; i < list.size(); ++i) { + for (int i = 0; i < list.size(); ++i) { NbtCompound c = list.getCompound(i); - Identifier id = getIdFromNbt(c); - if(id == null) { - continue; - } - ret.put(id, getLevelFromNbt(c)); + Identifier id = EnchantmentHelper.getIdFromNbt(c); + if (id == null) continue; + ret.put(id, EnchantmentHelper.getLevelFromNbt(c)); } list = stack.getNbt().getList("Enchantments", NbtElement.COMPOUND_TYPE); - for(int i = 0; i < list.size(); ++i) { + for (int i = 0; i < list.size(); ++i) { NbtCompound c = list.getCompound(i); - Identifier id = getIdFromNbt(c); - if(id == null) { - continue; - } - ret.put(id, getLevelFromNbt(c)); + Identifier id = EnchantmentHelper.getIdFromNbt(c); + if (id == null) continue; + ret.put(id, EnchantmentHelper.getLevelFromNbt(c)); } return ret; } public void triggerTradeCheck(TradeOfferList l) { - if (currentState != State.RollingWaitingForVillagerTrades) { - return; - } + if (currentState != State.RollingWaitingForVillagerTrades) return; for (TradeOffer offer : l) { // info(String.format("Offer: %s", offer.getSellItem().toString())); ItemStack sellItem = offer.getSellItem(); @@ -616,13 +606,9 @@ public void triggerTradeCheck(TradeOfferList l) { // level enchant.getValue() // enchantment enchant.getKey() boolean found = false; - for (rollingEnchantment e : searchingEnchants) { - if (!e.enabled) { - continue; - } - if (!e.enchantment.toString().equals(enchantIdString)) { - continue; - } + for (RollingEnchantment e : searchingEnchants) { + if (!e.enabled) continue; + if (!e.enchantment.toString().equals(enchantIdString)) continue; found = true; if (e.minLevel <= 0) { int ml = e.getMaxLevel(); @@ -631,35 +617,28 @@ public void triggerTradeCheck(TradeOfferList l) { enchantName, ml, enchantLevel)); continue; } - } else { - if (e.minLevel > enchantLevel) { - info(String.format("Found enchant %s but it has too low level: %d (requested level) > %d (rolled level)", - enchantName, e.minLevel, enchantLevel)); - continue; - } + } else if (e.minLevel > enchantLevel) { + info(String.format("Found enchant %s but it has too low level: %d (requested level) > %d (rolled level)", + enchantName, e.minLevel, enchantLevel)); + continue; } if (e.maxCost > 0 && offer.getOriginalFirstBuyItem().getCount() > e.maxCost) { info(String.format("Found enchant %s but it costs too much: %s (max price) < %d (cost)", enchantName, e.maxCost, offer.getOriginalFirstBuyItem().getCount())); continue; } - if (disableIfFound.get()) { - e.enabled = false; - } + if (disableIfFound.get()) e.enabled = false; toggle(); - if (enablePlaySound.get() && sound.get().size() > 0) { - mc.getSoundManager().play(PositionedSoundInstance.master(this.sound.get().get(0), - soundPitch.get().floatValue(), soundVolume.get().floatValue())); + if (enablePlaySound.get() && !sound.get().isEmpty()) { + mc.getSoundManager().play(PositionedSoundInstance.master(sound.get().get(0), + soundPitch.get().floatValue(), soundVolume.get().floatValue())); } break; } - if (!found) { - info(String.format("Found enchant %s but it is not in the list.", enchantName)); - } + if (!found) info(String.format("Found enchant %s but it is not in the list.", enchantName)); } } // ((MerchantScreenHandler)mc.player.currentScreenHandler).closeHandledScreen(); - assert mc.player != null; mc.player.closeHandledScreen(); currentState = State.RollingBreakingBlock; } @@ -668,7 +647,6 @@ public void triggerTradeCheck(TradeOfferList l) { private void onStartBreakingBlockEvent(StartBreakingBlockEvent event) { if (currentState == State.WaitingForTargetBlock) { rollingBlockPos = event.blockPos; - assert mc.world != null; rollingBlock = mc.world.getBlockState(rollingBlockPos).getBlock(); currentState = State.WaitingForTargetVillager; info("Rolling block selected, now interact with villager you want to roll"); @@ -681,146 +659,141 @@ private void placeFailed(String msg) { info(msg); failedToPlacePrevMsg = System.currentTimeMillis(); } - if (failedToPlaceDisable.get()) { - toggle(); - } + if (failedToPlaceDisable.get()) toggle(); } @EventHandler private void onTick(TickEvent.Pre event) { - assert mc.world != null; - if (currentState == State.RollingBreakingBlock) { - if (mc.world.getBlockState(rollingBlockPos) == Blocks.AIR.getDefaultState()) { - // info("Block is broken, waiting for villager to clean profession..."); - currentState = State.RollingWaitingForVillagerProfessionClear; - } else { - if (!BlockUtils.breakBlock(rollingBlockPos, true)) { + switch (currentState) { + case RollingBreakingBlock -> { + if (mc.world.getBlockState(rollingBlockPos) == Blocks.AIR.getDefaultState()) { + // info("Block is broken, waiting for villager to clean profession..."); + currentState = State.RollingWaitingForVillagerProfessionClear; + } else if (!BlockUtils.breakBlock(rollingBlockPos, true)) { info("Can not break block"); toggle(); } } - } else if (currentState == State.RollingWaitingForVillagerProfessionClear) { - if (mc.world.getBlockState(rollingBlockPos).isOf(Blocks.LECTERN)) { - info("Rolling block mining reverted?"); - currentState = State.RollingBreakingBlock; - return; - } - if (rollingVillager.getVillagerData().getProfession() == VillagerProfession.NONE) { - // info("Profession cleared"); - currentState = State.RollingPlacingBlock; - } - } else if (currentState == State.RollingPlacingBlock) { - FindItemResult item = InvUtils.findInHotbar(rollingBlock.asItem()); - if (!item.found()) { - placeFailed("Lectern not found in hotbar"); - return; - } - if (!BlockUtils.canPlace(rollingBlockPos, true)) { - placeFailed("Can't place lectern"); - return; - } - if (!BlockUtils.place(rollingBlockPos, item, headRotateOnPlace.get(), 5)) { - placeFailed("Failed to place lectern"); - return; + case RollingWaitingForVillagerProfessionClear -> { + if (mc.world.getBlockState(rollingBlockPos).isOf(Blocks.LECTERN)) { + info("Rolling block mining reverted?"); + currentState = State.RollingBreakingBlock; + return; + } + if (rollingVillager.getVillagerData().getProfession() == VillagerProfession.NONE) { + // info("Profession cleared"); + currentState = State.RollingPlacingBlock; + } } - currentState = State.RollingWaitingForVillagerProfessionNew; - if (maxProfessionWaitTime.get() > 0) { - currentProfessionWaitTime = System.currentTimeMillis(); + case RollingPlacingBlock -> { + FindItemResult item = InvUtils.findInHotbar(rollingBlock.asItem()); + if (!item.found()) { + placeFailed("Lectern not found in hotbar"); + return; + } + if (!BlockUtils.canPlace(rollingBlockPos, true)) { + placeFailed("Can't place lectern"); + return; + } + if (!BlockUtils.place(rollingBlockPos, item, headRotateOnPlace.get(), 5)) { + placeFailed("Failed to place lectern"); + return; + } + currentState = State.RollingWaitingForVillagerProfessionNew; + if (maxProfessionWaitTime.get() > 0) { + currentProfessionWaitTime = System.currentTimeMillis(); + } } - } else if (currentState == State.RollingWaitingForVillagerProfessionNew) { - if (maxProfessionWaitTime.get() > 0) { - if (currentProfessionWaitTime + maxProfessionWaitTime.get() <= System.currentTimeMillis()) { + case RollingWaitingForVillagerProfessionNew -> { + if (maxProfessionWaitTime.get() > 0 && (currentProfessionWaitTime + maxProfessionWaitTime.get() <= System.currentTimeMillis())) { info("Villager did not take profession within the specified time"); currentState = State.RollingBreakingBlock; return; } + if (mc.world.getBlockState(rollingBlockPos) == Blocks.AIR.getDefaultState()) { + info("Lectern placement reverted by server (AC?)"); + currentState = State.RollingPlacingBlock; + return; + } + if (!mc.world.getBlockState(rollingBlockPos).isOf(Blocks.LECTERN)) { + info("Placed wrong block?!"); + currentState = State.RollingBreakingBlock; + return; + } + if (rollingVillager.getVillagerData().getProfession() != VillagerProfession.NONE) { + currentState = State.RollingWaitingForVillagerTrades; + triggerInteract(); + } } - if (mc.world.getBlockState(rollingBlockPos) == Blocks.AIR.getDefaultState()) { - info("Lectern placement reverted by server (AC?)"); - currentState = State.RollingPlacingBlock; - return; - } - if (!mc.world.getBlockState(rollingBlockPos).isOf(Blocks.LECTERN)) { - info("Placed wrong block?!"); - currentState = State.RollingBreakingBlock; - return; - } - if (rollingVillager.getVillagerData().getProfession() != VillagerProfession.NONE) { - currentState = State.RollingWaitingForVillagerTrades; - triggerInteract(); + default -> { + // Wait for another state } } } public String getEnchantmentName(Identifier id) { - String ret = id.toString(); - var e = Registries.ENCHANTMENT.getOrEmpty(id); - if (e.isPresent()) { - ret = Names.get(e.get()); - } - return ret; + return Registries.ENCHANTMENT.getOrEmpty(id) + .map(Names::get) + .orElse(id.toString()); } - public static class rollingEnchantment implements ISerializable { - public Identifier enchantment; - public int minLevel; - public int maxCost; - public boolean enabled; + public static class RollingEnchantment implements ISerializable { + private Identifier enchantment; + private int minLevel; + private int maxCost; + private boolean enabled; - public rollingEnchantment(Identifier _enchantment, int _minLevel, int _maxCost, boolean _enabled) { - this.enchantment = _enchantment; - this.minLevel = _minLevel; - this.maxCost = _maxCost; - this.enabled = _enabled; + public RollingEnchantment(Identifier enchantment, int minLevel, int maxCost, boolean enabled) { + this.enchantment = enchantment; + this.minLevel = minLevel; + this.maxCost = maxCost; + this.enabled = enabled; } - public rollingEnchantment() { - this.enchantment = Registries.ENCHANTMENT.getId(Enchantments.PROTECTION); - this.minLevel = 0; - this.maxCost = 0; - this.enabled = false; + public RollingEnchantment() { + enchantment = Registries.ENCHANTMENT.getId(Enchantments.PROTECTION); + minLevel = 0; + maxCost = 0; + enabled = false; } @Override public NbtCompound toTag() { NbtCompound tag = new NbtCompound(); - tag.putString("enchantment", this.enchantment.toString()); - tag.putInt("minLevel", this.minLevel); - tag.putInt("maxCost", this.maxCost); - tag.putBoolean("enabled", this.enabled); + tag.putString("enchantment", enchantment.toString()); + tag.putInt("minLevel", minLevel); + tag.putInt("maxCost", maxCost); + tag.putBoolean("enabled", enabled); return tag; } @Override - public rollingEnchantment fromTag(NbtCompound tag) { - this.enchantment = Identifier.tryParse(tag.getString("enchantment")); - this.minLevel = tag.getInt("minLevel"); - this.maxCost = tag.getInt("maxCost"); - this.enabled = tag.getBoolean("enabled"); + public RollingEnchantment fromTag(NbtCompound tag) { + enchantment = Identifier.tryParse(tag.getString("enchantment")); + minLevel = tag.getInt("minLevel"); + maxCost = tag.getInt("maxCost"); + enabled = tag.getBoolean("enabled"); return this; } public boolean isCustom() { - return !Registries.ENCHANTMENT.containsId(this.enchantment); + return !Registries.ENCHANTMENT.containsId(enchantment); } + public boolean isVanilla() { - return Registries.ENCHANTMENT.containsId(this.enchantment); + return Registries.ENCHANTMENT.containsId(enchantment); } + public int getMaxLevel() { - int maxlevel = 0; - var e = Registries.ENCHANTMENT.getOrEmpty(this.enchantment); - if (e.isPresent()) { - maxlevel = e.get().getMaxLevel(); - } - return maxlevel; + return Registries.ENCHANTMENT.getOrEmpty(enchantment) + .map(Enchantment::getMaxLevel) + .orElse(0); } + public String getName() { - String ret = this.enchantment.toString(); - var e = Registries.ENCHANTMENT.getOrEmpty(this.enchantment); - if (e.isPresent()) { - ret = Names.get(e.get()); - } - return ret; + return Registries.ENCHANTMENT.getOrEmpty(enchantment) + .map(Names::get) + .orElse(enchantment.toString()); } } }