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

Added a debug command and some logging to the RC anchors reloading #17

Merged
merged 2 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ repositories {
}
}

version = "1.6.5"
version = "1.6.6"
group = "com.mitchej123.hodgepodge"
archivesBaseName = "hodgepodge"
sourceCompatibility = 1.8
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/Hodgepodge.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mitchej123.hodgepodge;

import com.mitchej123.hodgepodge.core.HodgePodgeClient;
import com.mitchej123.hodgepodge.core.commands.DebugCommand;
import com.mitchej123.hodgepodge.core.util.AnchorAlarm;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.relauncher.Side;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -33,4 +35,8 @@ public void postInit(FMLPostInitializationEvent event) {
HodgePodgeClient.postInit();
}
}
@EventHandler
public void onServerStarting(FMLServerStartingEvent aEvent) {
aEvent.registerServerCommand(new DebugCommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.mitchej123.hodgepodge.core.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;

import com.mitchej123.hodgepodge.core.util.AnchorAlarm;

public class DebugCommand extends CommandBase {
@Override
public String getCommandName() {
return "hp";
}

@Override
public String getCommandUsage(ICommandSender sender) {
return "Usage: hp <subcommand>. Valid subcommands are: toggle, anchor.";
}
private void printHelp(ICommandSender sender) {
sender.addChatMessage(new ChatComponentText("Usage: hp <toggle|anchor>"));
sender.addChatMessage(new ChatComponentText("\"toggle anchordebug\" - toggles RC anchor debugging"));
sender.addChatMessage(new ChatComponentText("\"anchor list <player>\" - list RC anchors placed by the player (empty for current player)"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been "\"anchor list [player]\" - list RC anchors placed by the player (empty for current player)"

}
@Override
public List addTabCompletionOptions(ICommandSender sender, String[] ss) {
List<String> l = new ArrayList<>();
String test = ss.length == 0 ? "" : ss[0].trim();
if (ss.length == 0 || ss.length == 1 && (test.isEmpty() || Stream.of("toggle", "anchor").anyMatch(s -> s.startsWith(test)))) {
Stream.of("toggle", "anchor")
.filter(s -> test.isEmpty() || s.startsWith(test))
.forEach(l::add);
} else if (test.equals("toggle")) {
String test1 = ss[1].trim();
if (test1.isEmpty() || "anchordebug".startsWith(test1))
l.add("anchordebug");
} else if (test.equals("anchor")) {
String test1 = ss[1].trim();
if (test1.isEmpty() || "list".startsWith(test1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a check for situation, where admin types /hp anchor list then press tab, expecting to see a list of usernames as completion.

FYI use getListOfStringsMatchingLastWord(as, MinecraftServer.getServer().getAllUsernames()) to quickly get a list of possible completions of username.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

l.add("list");
}
return l;
}
@Override
public void processCommand(ICommandSender sender, String[] strings) {
if (strings.length < 1) {
printHelp(sender);
return;
}
switch (strings[0]) {
case "toggle":
if (strings.length < 2 || !strings[1].equals("anchordebug")) {
printHelp(sender);
return;
}
AnchorAlarm.AnchorDebug = !AnchorAlarm.AnchorDebug;
sender.addChatMessage(new ChatComponentText("Anchor debugging: " + AnchorAlarm.AnchorDebug));
break;
case "anchor":
if (strings.length < 2 || !strings[1].equals("list")) {
printHelp(sender);
return;
}
String playerName = strings.length > 2 ? strings[2] : sender.getCommandSenderName();
if (!AnchorAlarm.listSavedAnchors(playerName, sender.getEntityWorld()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use net.minecraft.server.management.ServerConfigurationManager#playerEntityList to find out all players. Not sure if these are equivalent, just saying.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAllUsernames literally calls serverConfigurationManager

sender.addChatMessage(new ChatComponentText("No such player entity in the current world : " + playerName));
else
sender.addChatMessage(new ChatComponentText("Saved anchors dumped to the log for player: " + playerName));
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
import java.util.HashSet;
import java.util.Set;

import org.lwjgl.Sys;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;

import net.minecraftforge.common.DimensionManager;

import com.mitchej123.hodgepodge.Hodgepodge;
import com.mojang.authlib.GameProfile;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import io.netty.buffer.ByteBuf;
Expand All @@ -21,6 +28,7 @@

public class AnchorAlarm {
private static final String NBT_KEY = "GT_RC_AnchorAlarmList";
public static boolean AnchorDebug = false;
public static void addNewAnchor(EntityLivingBase entityliving, TileEntity te) {
if (entityliving instanceof EntityPlayerMP) {
byte[] oldbuf = null;
Expand All @@ -34,6 +42,30 @@ public static void addNewAnchor(EntityLivingBase entityliving, TileEntity te) {
saveCoordinatesToPlayer(entityliving, newbufwrap, newbuf, te);
}
}
public static boolean listSavedAnchors(String playerName, World w) {
for (Object obj : w.playerEntities)
{
if (((EntityPlayer)obj).getDisplayName().equals(playerName)) {
NBTTagCompound nbt = ((EntityPlayer)obj).getEntityData();
if (!nbt.hasKey(NBT_KEY)){
Hodgepodge.log.debug("[AnchorDebug] No anchors listed for player " + playerName);
} else {
byte[] bytes = nbt.getByteArray(NBT_KEY);
ByteBuf buf = Unpooled.wrappedBuffer(bytes);
int N = bytes.length / 16;
for (int i = 0; i < N; ++i) {
int dim = buf.readInt();
int x = buf.readInt();
int y = buf.readInt();
int z = buf.readInt();
Hodgepodge.log.debug("[AnchorDebug] Anchor (" + x + ", " + y + ", " + z + ") at dim " + dim + " for player " + playerName);
}
}
return true;
}
}
return false;
}
private static void saveCoordinatesToPlayer(EntityLivingBase player, ByteBuf buf, byte[] bytes, TileEntity te) {
buf.writeInt(te.getWorldObj().provider.dimensionId);
buf.writeInt(te.xCoord);
Expand All @@ -45,6 +77,9 @@ private static void saveCoordinatesToPlayer(EntityLivingBase player, ByteBuf buf
@SubscribeEvent
public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.player instanceof EntityPlayerMP) {
if (AnchorDebug) {
Hodgepodge.log.debug("[AnchorDebug] Loading anchors for player " + event.player.getDisplayName());
}
if (event.player.getEntityData().hasKey(NBT_KEY)) {
byte[] bytes = event.player.getEntityData().getByteArray(NBT_KEY);
ByteBuf buf = Unpooled.wrappedBuffer(bytes);
Expand All @@ -60,16 +95,20 @@ public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
int z = buf.readInt();
WorldServer w = DimensionManager.getWorld(dim);
if (w == null) {
if (AnchorDebug)
System.out.println("[AnchorDebug] Loading dimension " + dim);
DimensionManager.initDimension(dim);
w = DimensionManager.getWorld(dim);
}
if (w != null) {
// if there is some different tile at the place, ok, we will load this chunk one last time
w.getChunkProvider().loadChunk(x >> 4, z >> 4);
w.getChunkProvider().provideChunk(x >> 4, z >> 4);
TileEntity t = w.getTileEntity(x, y, z);
if (loadedTiles.contains(t))
continue;
loadedTiles.add(t);
if (AnchorDebug)
System.out.println("[AnchorDebug] Loading anchor at (" + x + ", " + y + ", " + z + ") at dim " + dim);
if (t instanceof TileAnchorWorld) {
if (PlayerPlugin.isSamePlayer(((TileAnchorWorld)t).getOwner(), event.player.getGameProfile())) {
// if there is still our tile there, save it for later
Expand All @@ -79,8 +118,15 @@ public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
newbuf.writeInt(y);
newbuf.writeInt(z);
}
else if (AnchorDebug) {
System.out.println("[AnchorDebug] Someone else\'s anchor at (" + x + ", " + y + ", " + z + ") at dim " + dim);
}
} else if (AnchorDebug){
System.out.println("[AnchorDebug] Failed loading anchor at (" + x + ", " + y + ", " + z + ") at dim " + dim);
}
}
else if (AnchorDebug)
System.out.println("[AnchorDebug] Failed loading dimension " + dim);
}
}
catch (IndexOutOfBoundsException ignored) {
Expand All @@ -90,6 +136,10 @@ public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
newbuf.readBytes(newbytes);
event.player.getEntityData().setByteArray(NBT_KEY, newbytes);
}
else {
if (AnchorDebug)
System.out.println("[AnchorDebug] No listed anchors for player " + event.player.getDisplayName());
}
}
}
}