Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module AutoEnchant #314

Merged
merged 10 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jar {
tasks.withType(Jar) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

def targetJavaVersion = 17
tasks.withType(JavaCompile).configureEach {
// ensure that the encoding is set to UTF-8, no matter what the system default is
// this fixes some edge cases with special characters not displaying correctly
Expand All @@ -90,3 +90,15 @@ tasks.withType(JavaCompile).configureEach {
it.options.release = targetVersion
}
}

java {
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
}
archivesBaseName = project.archives_base_name
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()
}
qingshu-ui marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/main/java/anticope/rejects/MeteorRejectsAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void onInitialize() {
modules.add(new SoundLocator());
modules.add(new TreeAura());
modules.add(new VehicleOneHit());
modules.add(new AutoEnchant());

// Commands
Commands.add(new CenterCommand());
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/anticope/rejects/modules/AutoEnchant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package anticope.rejects.modules;

import anticope.rejects.MeteorRejectsAddon;

import meteordevelopment.meteorclient.events.game.OpenScreenEvent;
import meteordevelopment.meteorclient.settings.*;
import meteordevelopment.meteorclient.utils.network.MeteorExecutor;
import meteordevelopment.meteorclient.utils.player.FindItemResult;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.screen.EnchantmentScreenHandler;
import net.minecraft.screen.ScreenHandler;

import java.util.List;
import java.util.Objects;

public class AutoEnchant extends meteordevelopment.meteorclient.systems.modules.Module {

public final SettingGroup sgGeneral = settings.getDefaultGroup();

private final Setting<Integer> delay = sgGeneral.add(new IntSetting.Builder()
.name("delay")
.description("The tick delay between enchanting items.")
qingshu-ui marked this conversation as resolved.
Show resolved Hide resolved
.defaultValue(50)
.sliderMax(500)
.min(0)
.build()
);

private final Setting<Integer> level = sgGeneral.add(new IntSetting.Builder()
.name("level")
.description("Choose enchantment levels 1-3")
.defaultValue(3)
.max(3)
.min(1)
.build()
);

private final Setting<Boolean> drop = sgGeneral.add(new BoolSetting.Builder()
.name("drop")
.description("Automatically drops enchanted items (useful for when not enough inventory space)")
.defaultValue(false)
.build()
);

private final Setting<List<Item>> itemWhitelist = sgGeneral.add(new ItemListSetting.Builder()
.name("item-whitelist")
.description("Item that require enchantment.")
.defaultValue()
.filter(item -> item.equals(Items.BOOK) || item.isDamageable())
.build()
);

public AutoEnchant() {
super(MeteorRejectsAddon.CATEGORY, "auto-enchant ", "Automatically enchanting items.");
qingshu-ui marked this conversation as resolved.
Show resolved Hide resolved
}

@EventHandler
private void onOpenScreen(OpenScreenEvent event) {
if (!(Objects.requireNonNull(mc.player).currentScreenHandler instanceof EnchantmentScreenHandler))
return;
MeteorExecutor.execute(this::autoEnchant);
}

private void autoEnchant() {
if (!(Objects.requireNonNull(mc.player).currentScreenHandler instanceof EnchantmentScreenHandler handler))
return;
if (mc.player.experienceLevel < 30) {
info("You don't have enough experience levels");
return;
}
while (getEmptySlotCount(handler) > 2 || drop.get()) {
if (!(mc.player.currentScreenHandler instanceof EnchantmentScreenHandler)) {
info("Enchanting table is closed.");
break;
}
if (handler.getLapisCount() < 3 && !fillLapisItem()) {
info("Lapis lazuli is not found.");
break;
}
if (!fillCanEnchantItem()) {
info("No items found to enchant.");
break;
}
Objects.requireNonNull(mc.interactionManager).clickButton(handler.syncId, level.get() - 1);
if (getEmptySlotCount(handler) > 2) {
InvUtils.shiftClick().slotId(0);
} else if (drop.get() && handler.getSlot(0).hasStack()) {
// I don't know why an exception LegacyRandomSource is thrown here,
// so I used the main thread to drop items.
mc.execute(() -> InvUtils.drop().slotId(0));
}

// Why sleep here? I don't know either.
try {
Thread.sleep(delay.get());
qingshu-ui marked this conversation as resolved.
Show resolved Hide resolved
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

private boolean fillCanEnchantItem() {
FindItemResult res = InvUtils.find(stack -> itemWhitelist.get().contains(stack.getItem()) && EnchantmentHelper.get(stack).isEmpty());
if (res.slot() == -1) return false;
qingshu-ui marked this conversation as resolved.
Show resolved Hide resolved
InvUtils.shiftClick().slot(res.slot());
return true;
}

private boolean fillLapisItem() {
FindItemResult res = InvUtils.find(Items.LAPIS_LAZULI);
if (res.slot() == -1) return false;
InvUtils.shiftClick().slot(res.slot());
return true;
}

private int getEmptySlotCount(ScreenHandler handler) {
int emptySlotCount = 0;
for (int i = 0; i < handler.slots.size(); i++) {
if (!handler.slots.get(i).getStack().getItem().equals(Items.AIR))
continue;
emptySlotCount++;
}
return emptySlotCount;
}

}