Skip to content

Commit

Permalink
Add tradable toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsupermanhd committed Jul 11, 2024
1 parent d48229e commit f224b1e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.registry.tag.EnchantmentTags;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.List;

import static meteordevelopment.meteorclient.MeteorClient.mc;
Expand All @@ -23,11 +24,13 @@ public class EnchantmentSelectScreen extends WindowScreen {
private final GuiTheme theme;
private final EnchantmentSelectCallback callback;
private String filterText = "";
private final boolean onlyTradable;

public EnchantmentSelectScreen(GuiTheme theme, EnchantmentSelectCallback callback) {
public EnchantmentSelectScreen(GuiTheme theme, boolean onlyTradable, EnchantmentSelectCallback callback) {
super(theme, "Select enchantment");
this.theme = theme;
this.callback = callback;
this.onlyTradable = onlyTradable;
}

public interface EnchantmentSelectCallback {
Expand Down Expand Up @@ -69,12 +72,18 @@ private void fillTable(WTable table) {
return;
}
var reg = mc.world.getRegistryManager().get(RegistryKeys.ENCHANTMENT);
List<RegistryEntry<Enchantment>> available;
var l = reg.getEntryList(EnchantmentTags.TRADEABLE);
if (l.isEmpty()) {
return;
List<RegistryEntry<Enchantment>> available = new ArrayList<>();
if (this.onlyTradable) {
var l = reg.getEntryList(EnchantmentTags.TRADEABLE);
if (l.isEmpty()) {
return;
}
available = l.get().stream().toList();
} else {
for (var a : reg.getIndexedEntries()) {
available.add(a);
}
}
available = l.get().stream().toList();
for (RegistryEntry<Enchantment> e : available.stream().sorted((o1, o2) -> Names.get(o1).compareToIgnoreCase(Names.get(o2))).toList()) {
if (!filterText.isEmpty() && !Names.get(e).toLowerCase().startsWith(filterText.toLowerCase())) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public class VillagerRoller extends Module {
.build()
);

private final Setting<Boolean> onlyTradable = sgGeneral.add(new BoolSetting.Builder()
.name("only-tradable")
.description("Hide enchantments that are not marked as tradable")
.defaultValue(false)
.build()
);

private final Setting<Boolean> sortEnchantments = sgGeneral.add(new BoolSetting.Builder()
.name("sort-enchantments")
.description("Show enchantments sorted by their name")
Expand Down Expand Up @@ -375,7 +382,7 @@ 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, onlyTradable.get(), sel -> {
searchingEnchants.set(si, sel);
list.clear();
fillWidget(theme, list);
Expand Down Expand Up @@ -427,7 +434,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) {
};

WButton add = controls.add(theme.button("Add")).expandX().widget();
add.action = () -> mc.setScreen(new EnchantmentSelectScreen(theme, e -> {
add.action = () -> mc.setScreen(new EnchantmentSelectScreen(theme, onlyTradable.get(), e -> {
e.minLevel = 1;
e.maxCost = 64;
e.enabled = true;
Expand All @@ -441,7 +448,7 @@ private void fillWidget(GuiTheme theme, WVerticalList list) {
list.clear();
searchingEnchants.clear();
if (reg != null) {
for (RegistryEntry<Enchantment> e : getTradableEnchants()) {
for (RegistryEntry<Enchantment> e : getEnchants(onlyTradable.get())) {
searchingEnchants.add(new RollingEnchantment(reg.getId(e.value()), e.value().getMaxLevel(), getMinimumPrice(e), true));
}
}
Expand Down Expand Up @@ -509,14 +516,21 @@ private void fillWidget(GuiTheme theme, WVerticalList list) {

}

public List<RegistryEntry<Enchantment>> getTradableEnchants() {
public List<RegistryEntry<Enchantment>> getEnchants(boolean onlyTradable) {
if (mc.world == null) {
return Collections.emptyList();
}
var reg = mc.world.getRegistryManager().get(RegistryKeys.ENCHANTMENT);
List<RegistryEntry<Enchantment>> available;
var l = reg.getEntryList(EnchantmentTags.TRADEABLE);
return l.map(registryEntries -> registryEntries.stream().toList()).orElse(Collections.emptyList());
List<RegistryEntry<Enchantment>> available = new ArrayList<>();
if (onlyTradable) {
var l = reg.getEntryList(EnchantmentTags.TRADEABLE);
return l.map(registryEntries -> registryEntries.stream().toList()).orElse(Collections.emptyList());
} else {
for (var a : reg.getIndexedEntries()) {
available.add(a);
}
return available;
}
}

public static int getMinimumPrice(RegistryEntry<Enchantment> e) {
Expand Down

0 comments on commit f224b1e

Please sign in to comment.