Skip to content

Commit

Permalink
saved templates improvements (code palette entry) and why logging eve…
Browse files Browse the repository at this point in the history
…ry command?
  • Loading branch information
GeorgeRNG committed Apr 4, 2024
1 parent 7df36ac commit 11173b0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
3 changes: 0 additions & 3 deletions src/main/java/dev/dfonline/codeclient/CodeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ public static <T extends PacketListener> boolean onSendPacket(Packet<T> packet)
if(BuildPhaser.onPacket(packet)) return true;
Event.onSendPacket(packet);
String name = packet.getClass().getName().replace("net.minecraft.network.packet.c2s.play.","");
if(packet instanceof CommandExecutionC2SPacket commandExecutionC2SPacket) {
LOGGER.info(commandExecutionC2SPacket.command());
}
// LOGGER.info(name);
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dfonline/codeclient/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ else if(!Files.isDirectory(currentPath)) {
return -2;
}
})));
dispatcher.register(literal("save").then(argument("path", StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
dispatcher.register(literal("save").then(argument("path", StringArgumentType.greedyString()).suggests(Commands::suggestDirectories).executes(context -> {
String data = Utility.templateDataItem(CodeClient.MC.player.getMainHandStack());
if(data == null) {
Utility.sendMessage("You need to hold a template to save.",ChatType.FAIL);
Expand Down Expand Up @@ -389,7 +389,7 @@ else if(!Files.isDirectory(currentPath)) {
try {
byte[] data = Files.readAllBytes(path);
ItemStack template = Utility.makeTemplate(new String(Base64.getEncoder().encode(data)));
template.setCustomName(Text.empty().formatted(Formatting.RED).append("Saved Template").append(Text.literal(" » ").formatted(Formatting.DARK_RED, Formatting.BOLD)).append(String.valueOf(path.getFileName())));
template.setCustomName(Text.empty().formatted(Formatting.RED).append("Saved Template").append(Text.literal(" » ").formatted(Formatting.DARK_RED, Formatting.BOLD)).append(String.valueOf(FileManager.templatesPath().relativize(path))));
CodeClient.MC.player.giveItemStack(template);
Utility.sendInventory();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package dev.dfonline.codeclient.dev.menu.DevInventory;

import dev.dfonline.codeclient.FileManager;
import dev.dfonline.codeclient.Utility;
import dev.dfonline.codeclient.hypercube.actiondump.*;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class DevInventoryGroup {
Expand Down Expand Up @@ -111,7 +118,7 @@ public int getDisplayY(int originY, int menuHeight) {
public static final DevInventoryGroup POTIONS = new DevInventoryGroup("potion", "Potions", Items.DRAGON_BREATH.getDefaultStack(), false);
public static final DevInventoryGroup GAME_VALUES = new DevInventoryGroup("game_value", "Game Values", Items.NAME_TAG.getDefaultStack(), false);
public static final DevInventoryGroup OTHERS = new DevInventoryGroup("others", "Other Items", Items.IRON_INGOT.getDefaultStack(), false).disableSearch();
public static final DevInventoryGroup CODE_VAULT = new DevInventoryGroup("code_vault", "Code Vault", Items.ENDER_CHEST.getDefaultStack(), false);
public static final DevInventoryGroup SAVED_TEMPLATES = new DevInventoryGroup("saved_templates", "Saved Templates", Items.ENDER_CHEST.getDefaultStack(), false);
public static final DevInventoryGroup INVENTORY = new DevInventoryGroup("inventory", "Inventory", Items.CHEST.getDefaultStack(), false).disableSearch();

interface ItemsProvider {
Expand Down Expand Up @@ -211,6 +218,18 @@ private DevInventoryGroup useCategory(Searchable[] categoryValues) {
POTIONS.useCategory(actionDump.potions);
PARTICLES.useCategory(actionDump.particles);

SAVED_TEMPLATES.setItemsProvider(query -> {
ArrayList<Searchable> items = new ArrayList<>();
if(query == null) query = "";
query = query.toLowerCase();
for (Searchable template: readTemplates(FileManager.templatesPath())) {
for (String term : template.getTerms()) if(term.toLowerCase().contains(query)) {
items.add(template);
break;
}
}
return items;
});
SEARCH.setItemsProvider(query -> {
ArrayList<Searchable> items = new ArrayList<>();
if(query == null) query = "";
Expand Down Expand Up @@ -246,4 +265,26 @@ private DevInventoryGroup useCategory(Searchable[] categoryValues) {
catch (Exception ignored) {
}
}

static private ArrayList<Searchable> readTemplates(Path path) {
return readTemplates(path,path);
}
static private ArrayList<Searchable> readTemplates(Path path, Path root) {
var list = new ArrayList<Searchable>();
try {
var dir = Files.list(path);
for (var file : dir.toList()) {
if(Files.isDirectory(file)) list.addAll(readTemplates(file,root));
if(file.getFileName().toString().endsWith(".dft")) {
byte[] data = Files.readAllBytes(file);
ItemStack template = Utility.makeTemplate(new String(Base64.getEncoder().encode(data)));
template.setCustomName(Text.empty().formatted(Formatting.RED).append("Saved Template").append(Text.literal(" » ").formatted(Formatting.DARK_RED, Formatting.BOLD)).append(String.valueOf(root.relativize(file))));
list.add(new Searchable.StaticSearchable(template));
}
}
dir.close();
} catch (IOException ignored) {
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
Integer hoveredGroup = getGroupFromMouse(mouseX,mouseY);
if(hoveredGroup != null) {
DevInventoryGroup group = GROUPS[hoveredGroup];
if(group != CODE_VAULT || GROUPS[selectedTab] == CODE_VAULT) context.drawTooltip(textRenderer, GROUPS[hoveredGroup].getName(), mouseX, mouseY);
context.drawTooltip(textRenderer, GROUPS[hoveredGroup].getName(), mouseX, mouseY);
}

if (this.deleteItemSlot != null && this.isPointWithinBounds(this.deleteItemSlot.x, this.deleteItemSlot.y, 16, 16, mouseX, mouseY)) {
Expand Down Expand Up @@ -364,9 +364,7 @@ protected void drawBackground(DrawContext context, float delta, int mouseX, int
for(DevInventoryGroup group : DevInventoryGroup.GROUPS) {
RenderSystem.setShader(GameRenderer::getPositionTexProgram);
RenderSystem.setShaderTexture(0, TEXTURE);
if (group.getIndex() != selectedTab && group != CODE_VAULT) {
this.renderTabIcon(context, group);
}
this.renderTabIcon(context, group);
}

RenderSystem.setShader(GameRenderer::getPositionTexProgram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minecraft.item.ItemStack;

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

public interface Searchable {
Expand All @@ -14,7 +13,7 @@ class StaticSearchable implements Searchable {
private final ItemStack item;

public StaticSearchable(ItemStack item) {
this.terms = new ArrayList<>();
this.terms = List.of(item.getName().getString());
this.item = item;
}

Expand Down

0 comments on commit 11173b0

Please sign in to comment.