From 3904749115fb7a71bf7c7f9d5d1530e1be2c115b Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Tue, 30 Jan 2024 20:47:17 +0100 Subject: more tutorial --- src/main/java/com/example/CrashCommand.java | 60 +++++++++++++++++++++- src/main/java/com/example/ExampleMod.java | 46 +++++++++++++++-- .../com/example/mixin/RightClickWithItemEvent.java | 14 +++++ 3 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/example/mixin/RightClickWithItemEvent.java (limited to 'src/main') diff --git a/src/main/java/com/example/CrashCommand.java b/src/main/java/com/example/CrashCommand.java index 516708d..4fb9cbb 100644 --- a/src/main/java/com/example/CrashCommand.java +++ b/src/main/java/com/example/CrashCommand.java @@ -1,13 +1,26 @@ package com.example; +import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.ScorePlayerTeam; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraft.util.BlockPos; +import net.minecraft.util.ChatComponentText; +import org.apache.logging.log4j.LogManager; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; public class CrashCommand extends CommandBase { @Override public String getCommandName() { - return "crashme"; + return "randomutils"; } @Override @@ -17,7 +30,50 @@ public class CrashCommand extends CommandBase { @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { - ExampleMod.screenToOpenNextTick = new MyGuiScreen(); + if (args.length == 0) { + sender.addChatMessage(new ChatComponentText("§cPlease use an argument")); + } else if (args[0].equals("weather")) { + sender.addChatMessage(new ChatComponentText("§bCurrent Weather: " + + (Minecraft.getMinecraft().theWorld.isRaining() ? "§7Rainy!" : "§eSunny!"))); + } else if (args[0].equals("coinflip")) { + sender.addChatMessage(new ChatComponentText("§bCoinflip: " + + (ThreadLocalRandom.current().nextBoolean() ? "§eHeads" : "§eTails"))); + } else if (args[0].equals("scoreboard")) { + final int SIDEBAR_SLOT = 1; + + Scoreboard scoreboard = Minecraft.getMinecraft().theWorld.getScoreboard(); + ScoreObjective objective = scoreboard.getObjectiveInDisplaySlot(SIDEBAR_SLOT); + List scoreList = scoreboard.getSortedScores(objective) + .stream() + .limit(15) + .map(score -> + ScorePlayerTeam.formatPlayerName( + scoreboard.getPlayersTeam(score.getPlayerName()), + score.getPlayerName())) + .map(text -> { + StringBuilder sb = new StringBuilder(); + for (char c : text.toCharArray()) { + if (Minecraft.getMinecraft().fontRendererObj.getCharWidth(c) > 0 || c == '§') + sb.append(c); + } + return sb.toString(); + }) + .collect(Collectors.toList()); + Collections.reverse(scoreList); + for (String s : scoreList) { + LogManager.getLogger("Scoreboard").info(s); + sender.addChatMessage(new ChatComponentText(s)); + } + } else { + sender.addChatMessage(new ChatComponentText("§cUnknown subcommand")); + } + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { + if (args.length == 1) + return getListOfStringsMatchingLastWord(args, "weather", "coinflip", "scoreboard"); + return Arrays.asList(); } @Override diff --git a/src/main/java/com/example/ExampleMod.java b/src/main/java/com/example/ExampleMod.java index 166c371..6a386e1 100644 --- a/src/main/java/com/example/ExampleMod.java +++ b/src/main/java/com/example/ExampleMod.java @@ -2,12 +2,25 @@ package com.example; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.inventory.GuiChest; +import net.minecraft.inventory.ContainerChest; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.nbt.NBTTagString; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; @Mod(modid = "examplemod", useMetadata = true) public class ExampleMod { @@ -24,11 +37,38 @@ public class ExampleMod { public static final int MOUSE_MIDDLE = 2; public static final int MOUSE_BACKWARD = 3; public static final int MOUSE_FORWARD = 4; + + public static List listFromNBT(NBTTagList nbtList, Function reader) { + List ts = new ArrayList<>(nbtList.tagCount()); + for (int i = 0; i < nbtList.tagCount(); i++) { + //noinspection unchecked + ts.add(reader.apply((U) nbtList.get(i))); + } + return ts; + } + @SubscribeEvent public void onTick(TickEvent.ClientTickEvent event) { - if (screenToOpenNextTick != null) { - Minecraft.getMinecraft().displayGuiScreen(screenToOpenNextTick); - screenToOpenNextTick = null; + if (event.phase != TickEvent.Phase.END) return; + GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen; + if (!(currentScreen instanceof GuiChest)) return; + ContainerChest container = (ContainerChest) ((GuiChest) currentScreen).inventorySlots; + LogManager.getLogger("ExampleMod").info("Container Name: " + container.getLowerChestInventory().getDisplayName().getFormattedText()); + Logger logger = LogManager.getLogger("ExampleMod"); + for (int i = 0; i < container.getLowerChestInventory().getSizeInventory(); i++) { + ItemStack stack = container.getLowerChestInventory().getStackInSlot(i); + if (stack == null) continue; + logger.info("Slot " + i + ":"); + + byte STRING_NBT_TAG = new NBTTagString().getId(); + NBTTagCompound tagCompound = stack.getTagCompound(); + if (tagCompound == null) continue; + String displayName = tagCompound.getCompoundTag("display").getString("Name"); + NBTTagList loreList = tagCompound.getCompoundTag("display").getTagList("Lore", STRING_NBT_TAG); + List loreStrings = listFromNBT(loreList, NBTTagString::getString); + for (int i1 = 0; i1 < loreList.tagCount(); i1++) { + String loreLine = loreList.getStringTagAt(i1); + } } } } diff --git a/src/main/java/com/example/mixin/RightClickWithItemEvent.java b/src/main/java/com/example/mixin/RightClickWithItemEvent.java new file mode 100644 index 0000000..c1686cb --- /dev/null +++ b/src/main/java/com/example/mixin/RightClickWithItemEvent.java @@ -0,0 +1,14 @@ +package com.example.mixin; + +import net.minecraft.client.multiplayer.PlayerControllerMP; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +import java.awt.*; + +// This Mixin targets the Minecraft class +@Mixin(PlayerControllerMP.class) +public class RightClickWithItemEvent { + @Unique + Color color_mymod = Color.RED; +} -- cgit