added some commands to log debug information about pretty pipes and clear invalid caches

This commit is contained in:
Ell 2022-02-11 21:56:02 +01:00
parent 33f4d47889
commit d04dd4054a
4 changed files with 69 additions and 1 deletions

View file

@ -2,12 +2,20 @@ package de.ellpeck.prettypipes.misc;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
@Mod.EventBusSubscriber @Mod.EventBusSubscriber
public final class Events { public final class Events {
@ -15,4 +23,34 @@ public final class Events {
public static void onWorldCaps(AttachCapabilitiesEvent<Level> event) { public static void onWorldCaps(AttachCapabilitiesEvent<Level> event) {
event.addCapability(new ResourceLocation(PrettyPipes.ID, "network"), new PipeNetwork(event.getObject())); event.addCapability(new ResourceLocation(PrettyPipes.ID, "network"), new PipeNetwork(event.getObject()));
} }
@SubscribeEvent
public static void onServerStarting(ServerStartingEvent event) {
event.getServer().getCommands().getDispatcher().register(Commands.literal(PrettyPipes.ID).requires(s -> s.hasPermission(2))
.then(Commands.literal("dump").executes(c -> {
var source = c.getSource();
var file = Paths.get('_' + PrettyPipes.ID + "dump.txt");
var dump = PipeNetwork.get(source.getLevel()).toString();
try {
Files.writeString(file, dump, StandardCharsets.UTF_8);
source.sendSuccess(new TextComponent("Wrote network dump to file " + file.toAbsolutePath()), true);
} catch (IOException e) {
source.sendFailure(new TextComponent("Failed to write network dump to file " + file.toAbsolutePath()));
return -1;
}
return 0;
}))
.then(Commands.literal("uncache").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).clearCaches();
source.sendSuccess(new TextComponent("Cleared all pipe caches in the world"), true);
return 0;
}))
.then(Commands.literal("unlock").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).unlock();
source.sendSuccess(new TextComponent("Resolved all network locks in the world"), true);
return 0;
})));
}
} }

View file

@ -51,4 +51,9 @@ public class NetworkLock implements INBTSerializable<CompoundTag> {
public int hashCode() { public int hashCode() {
return Objects.hash(this.lockId); return Objects.hash(this.lockId);
} }
@Override
public String toString() {
return "NetworkLock{" + "location=" + this.location.pipePos + ", stack=" + this.stack + '}';
}
} }

View file

@ -317,6 +317,11 @@ public class PipeItem implements IPipeItem {
} }
} }
@Override
public String toString() {
return "PipeItem{" + "stack=" + this.stack + ", x=" + this.x + ", y=" + this.y + ", z=" + this.z + ", startInventory=" + this.startInventory + ", destInventory=" + this.destInventory + '}';
}
protected int getModelCount() { protected int getModelCount() {
var i = 1; var i = 1;
if (this.stack.getCount() > 48) { if (this.stack.getCount() > 48) {

View file

@ -245,7 +245,8 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
var tile = this.tileCache.get(pos); var tile = this.tileCache.get(pos);
if (tile == null || tile.isRemoved()) { if (tile == null || tile.isRemoved()) {
tile = Utility.getBlockEntity(PipeBlockEntity.class, this.world, pos); tile = Utility.getBlockEntity(PipeBlockEntity.class, this.world, pos);
this.tileCache.put(pos, tile); if (tile != null)
this.tileCache.put(pos, tile);
} }
return tile; return tile;
} }
@ -387,6 +388,15 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
return null; return null;
} }
public void clearCaches() {
this.nodeToConnectedNodes.clear();
this.tileCache.clear();
}
public void unlock() {
this.networkLocks.clear();
}
private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) { private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) {
this.startProfile("create_all_edges"); this.startProfile("create_all_edges");
List<NetworkEdge> edges = new ArrayList<>(); List<NetworkEdge> edges = new ArrayList<>();
@ -506,6 +516,16 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public void vertexRemoved(GraphVertexChangeEvent<BlockPos> e) { public void vertexRemoved(GraphVertexChangeEvent<BlockPos> e) {
} }
@Override
public String toString() {
return "PipeNetwork{" +
"\ngraph=" + this.graph +
",\nnodeToConnectedNodes=" + this.nodeToConnectedNodes +
",\ntileCache=" + this.tileCache.keySet() +
",\npipeItems=" + this.pipeItems +
",\nnetworkLocks=" + this.networkLocks + '}';
}
public void startProfile(String name) { public void startProfile(String name) {
this.world.getProfiler().push(() -> PrettyPipes.ID + ":pipe_network_" + name); this.world.getProfiler().push(() -> PrettyPipes.ID + ":pipe_network_" + name);
} }