Skip to content

Commit 9bdcab5

Browse files
committed
Improve list command
1 parent 12a6277 commit 9bdcab5

File tree

5 files changed

+151
-68
lines changed

5 files changed

+151
-68
lines changed

src/main/java/com/hlysine/create_power_loader/command/ListLoadersCommand.java

Lines changed: 110 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.minecraft.network.chat.ClickEvent;
2121
import net.minecraft.network.chat.Component;
2222
import net.minecraft.network.chat.HoverEvent;
23+
import net.minecraft.network.chat.MutableComponent;
2324
import net.minecraft.resources.ResourceKey;
2425
import net.minecraft.resources.ResourceLocation;
2526
import net.minecraft.server.MinecraftServer;
@@ -39,45 +40,66 @@ public class ListLoadersCommand {
3940
public static ArgumentBuilder<CommandSourceStack, ?> register() {
4041
return Commands.literal("list")
4142
.requires(cs -> cs.hasPermission(2))
42-
.then(
43-
Commands.argument("type", EnumArgument.enumArgument(LoaderMode.class))
44-
.then(
45-
Commands.literal("limit")
46-
.then(
47-
Commands.argument("limit", IntegerArgumentType.integer(1))
48-
.executes(handler(true, true))
49-
)
50-
)
51-
.executes(handler(true, false))
43+
.then(Commands.argument("type", EnumArgument.enumArgument(LoaderMode.class))
44+
.then(Commands.literal("active")
45+
.then(Commands.literal("limit")
46+
.then(Commands.argument("limit", IntegerArgumentType.integer(1))
47+
.executes(handler(true, true, true))
48+
)
49+
).executes(handler(true, false, true))
50+
)
51+
.then(Commands.literal("all")
52+
.then(Commands.literal("limit")
53+
.then(Commands.argument("limit", IntegerArgumentType.integer(1))
54+
.executes(handler(true, true, false))
55+
)
56+
).executes(handler(true, false, false))
57+
)
5258
)
53-
.then(
54-
Commands.literal("limit")
55-
.then(
56-
Commands.argument("limit", IntegerArgumentType.integer(1))
57-
.executes(handler(false, true))
58-
)
59-
)
60-
.executes(handler(false, false));
59+
.then(Commands.literal("all")
60+
.then(Commands.literal("active")
61+
.then(Commands.literal("limit")
62+
.then(Commands.argument("limit", IntegerArgumentType.integer(1))
63+
.executes(handler(false, true, true))
64+
)
65+
).executes(handler(false, false, true))
66+
)
67+
.then(Commands.literal("all")
68+
.then(Commands.literal("limit")
69+
.then(Commands.argument("limit", IntegerArgumentType.integer(1))
70+
.executes(handler(false, true, false))
71+
)
72+
).executes(handler(false, false, false))
73+
)
74+
);
6175
}
6276

63-
private static Command<CommandSourceStack> handler(boolean hasMode, boolean hasLimit) {
77+
private static Command<CommandSourceStack> handler(boolean hasMode, boolean hasLimit, boolean activeOnly) {
6478
return ctx -> {
6579
CommandSourceStack source = ctx.getSource();
6680
fillReport(source.getLevel(), source.getPosition(),
6781
hasMode ? ctx.getArgument("type", LoaderMode.class) : null,
68-
hasLimit ? ctx.getArgument("limit", Integer.class) : Integer.MAX_VALUE,
82+
hasLimit ? ctx.getArgument("limit", Integer.class) : 20,
83+
activeOnly,
6984
(s, f) -> source.sendSuccess(() -> Components.literal(s).withStyle(st -> st.withColor(f)), false),
7085
(c) -> source.sendSuccess(() -> c, false));
7186
return Command.SINGLE_SUCCESS;
7287
};
7388
}
7489

75-
private static void fillReport(ServerLevel level, Vec3 location, @Nullable LoaderMode mode, int limit, BiConsumer<String, Integer> chat,
90+
private static void fillReport(ServerLevel level,
91+
Vec3 location,
92+
@Nullable LoaderMode mode,
93+
int limit,
94+
boolean activeOnly,
95+
BiConsumer<String, Integer> chat,
7696
Consumer<Component> chatRaw) {
7797
int white = ChatFormatting.WHITE.getColor();
98+
int gray = ChatFormatting.GRAY.getColor();
7899
int blue = 0xD3DEDC;
79-
int bright = 0xFFEFEF;
100+
int darkBlue = 0x5955A1;
80101
int orange = 0xFFAD60;
102+
int darkOrange = 0xB27943;
81103

82104
List<ChunkLoader> loaders = new LinkedList<>();
83105
if (mode == null) {
@@ -87,7 +109,20 @@ private static void fillReport(ServerLevel level, Vec3 location, @Nullable Loade
87109
} else {
88110
loaders.addAll(ChunkLoadManager.allLoaders.get(mode));
89111
}
90-
loaders.removeIf(loader -> loader.getForcedChunks().size() == 0);
112+
loaders.removeIf(loader -> {
113+
if (loader instanceof TrainChunkLoader trainLoader) {
114+
for (CarriageChunkLoader carriageLoader : trainLoader.carriageLoaders) {
115+
if (carriageLoader.known && (carriageLoader.brass || carriageLoader.andesite)) return false;
116+
}
117+
return true;
118+
} else if (loader instanceof StationChunkLoader stationLoader) {
119+
return stationLoader.attachments.size() == 0;
120+
} else {
121+
return false;
122+
}
123+
});
124+
if (activeOnly)
125+
loaders.removeIf(loader -> loader.getForcedChunks().size() == 0);
91126

92127
Map<ResourceLocation, DimensionType> typeCache = new HashMap<>();
93128
MinecraftServer server = level.getServer();
@@ -106,62 +141,78 @@ private static void fillReport(ServerLevel level, Vec3 location, @Nullable Loade
106141

107142
chat.accept("", white);
108143
chat.accept("-+------<< Chunk Loader List >>------+-", white);
109-
chat.accept(pairs.size() + " out of " + loaders.size() + " nearest" + (mode != null ? " " + mode.getSerializedName() : "") + " loaders", blue);
144+
chat.accept(pairs.size() + " out of " + loaders.size() + " nearest" + (activeOnly ? " active" : "") + (mode != null ? " " + mode.getSerializedName() : "") + " loaders", blue);
145+
chat.accept("", white);
110146
for (Pair<ChunkLoader, Pair<ResourceLocation, Vec3>> pair : pairs) {
111147
ChunkLoader loader = pair.getFirst();
112148
ResourceLocation dimension = pair.getSecond().getFirst();
113149
BlockPos pos = BlockPos.containing(pair.getSecond().getSecond());
114150

115-
chatRaw.accept(createTpButton(dimension, pos,
116-
(mode == null ? " " + loader.getLoaderMode().getSerializedName() + " " : "")
117-
+ "[" + pos.toShortString() + "]"
118-
+ (!dimension.equals(level.dimension().location()) ? " in " + dimension : "")
119-
, white));
120-
121-
chat.accept(
122-
" "
123-
+ loader.getLoaderType().getSerializedName() + " - "
124-
+ loader.getForcedChunks().size() + " chunks"
125-
, orange);
151+
chatRaw.accept(
152+
text(mode == null ? loader.getLoaderMode().getSerializedName() + " - " : "", white)
153+
.append(text(loader.getLoaderType().getSerializedName() + " - ", orange))
154+
.append(text(loader.getForcedChunks().size() + " chunks", colorForCount(loader.getForcedChunks().size())))
155+
);
156+
157+
chatRaw.accept(
158+
text(" ↳ ", darkBlue)
159+
.append(createTpButton(level.dimension().location(), dimension, pos, darkBlue))
160+
);
126161
if (loader instanceof TrainChunkLoader trainLoader) {
127162
for (int i = 0; i < trainLoader.carriageLoaders.size(); i++) {
128163
CarriageChunkLoader carriageLoader = trainLoader.carriageLoaders.get(i);
129164
if (carriageLoader.getForcedChunks().isEmpty()) continue;
130165
Pair<ResourceLocation, BlockPos> carriageLocation = carriageLoader.getLocation();
131-
chatRaw.accept(createTpButton(carriageLocation.getFirst(), carriageLocation.getSecond(),
132-
" Carriage " + (i + 1) + " - "
133-
+ "[" + carriageLocation.getSecond().toShortString() + "]"
134-
+ (!carriageLocation.getFirst().equals(level.dimension().location()) ? " in " + carriageLocation.getFirst().toString() : "")
135-
, blue));
136-
chat.accept(
137-
" "
138-
+ carriageLoader.getLoaderType().getSerializedName() + " - "
139-
+ carriageLoader.getForcedChunks().size() + " chunks"
140-
, orange);
166+
chatRaw.accept(
167+
text(" Carriage " + (i + 1) + " - ", gray)
168+
.append(text(carriageLoader.getLoaderType().getSerializedName() + " - ", darkOrange))
169+
.append(text(carriageLoader.getForcedChunks().size() + " chunks", colorForCount(carriageLoader.getForcedChunks().size())))
170+
);
171+
chatRaw.accept(
172+
text(" ↳ ", darkBlue)
173+
.append(createTpButton(level.dimension().location(), carriageLocation.getFirst(), carriageLocation.getSecond(), darkBlue))
174+
);
141175
}
142176
} else if (loader instanceof StationChunkLoader stationLoader) {
143177
for (StationChunkLoader.AttachedLoader attachment : stationLoader.attachments) {
144-
chatRaw.accept(createTpButton(stationLoader.getLocation().getFirst(), attachment.pos(),
145-
" "
146-
+ attachment.type().getSerializedName()
147-
+ " - "
148-
+ "[" + attachment.pos().toShortString() + "]"
149-
, blue));
178+
chatRaw.accept(
179+
text(" ", gray)
180+
.append(text("Attached - ", gray))
181+
.append(text(attachment.type().getSerializedName(), darkOrange))
182+
);
183+
chatRaw.accept(
184+
text(" ↳ ", darkBlue)
185+
.append(createTpButton(level.dimension().location(), stationLoader.getLocation().getFirst(), attachment.pos(), darkBlue))
186+
);
150187
}
151188
}
152189
}
153190
chat.accept("-+--------------------------------+-", white);
154191
}
155192

156-
private static Component createTpButton(ResourceLocation dimension, BlockPos blockPos, String text, int color) {
193+
private static int colorForCount(int count) {
194+
if (count == 0) return ChatFormatting.DARK_GRAY.getColor();
195+
if (count < 5) return ChatFormatting.GRAY.getColor();
196+
if (count < 10) return ChatFormatting.YELLOW.getColor();
197+
return ChatFormatting.RED.getColor();
198+
}
199+
200+
private static String shortString(ResourceLocation location) {
201+
if (location.getNamespace().equals(ResourceLocation.DEFAULT_NAMESPACE)) return location.getPath();
202+
return location.toString();
203+
}
204+
205+
private static MutableComponent text(String text, int color) {
206+
return Components.literal(text).withStyle(style -> style.withColor(color));
207+
}
208+
209+
private static MutableComponent createTpButton(ResourceLocation origin, ResourceLocation dimension, BlockPos blockPos, int color) {
157210
String teleport = "/execute in " + dimension.toString() + " run tp @s " + blockPos.getX() + " " + blockPos.getY() + " " + blockPos.getZ();
158-
return Components.literal(text).withStyle((p_180514_) -> {
159-
return p_180514_.withColor(color)
160-
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, teleport))
161-
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
162-
Components.literal("Click to teleport")))
163-
.withInsertion(teleport);
164-
});
211+
return Components.literal("[" + blockPos.toShortString() + "]" + (!origin.equals(dimension) ? " in " + shortString(dimension) : "")).withStyle((style) -> style
212+
.withColor(color)
213+
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, teleport))
214+
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Components.literal("Click to teleport")))
215+
.withInsertion(teleport));
165216
}
166217

167218
}

src/main/java/com/hlysine/create_power_loader/content/AbstractChunkLoaderBlockEntity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public void updateAttachedStation(StationBlockEntity be) {
8585
deferredEdgePoint = true; // The GlobalStation is only created in the next tick after the station block is placed
8686
}
8787
} else {
88-
addToManager();
88+
if (!level.isClientSide())
89+
addToManager();
8990
}
9091
}
9192

@@ -101,7 +102,8 @@ public void initialize() {
101102
if (!(be instanceof StationBlockEntity sbe)) return;
102103
updateAttachedStation(sbe);
103104
} else {
104-
addToManager();
105+
if (!level.isClientSide())
106+
addToManager();
105107
}
106108
}
107109

src/main/java/com/hlysine/create_power_loader/content/ChunkLoaderMovementBehaviour.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void startMoving(MovementContext context) {
4040
Object tempState = context.temporaryData;
4141

4242
if (!(tempState instanceof SavedState)) {
43-
tempState = new SavedState(type, null, null, new HashSet<>());
43+
tempState = new SavedState(type, null, null, context.contraption instanceof CarriageContraption, new HashSet<>());
4444
}
4545

4646
SavedState savedState = (SavedState) tempState;
@@ -62,7 +62,7 @@ public void visitNewPosition(MovementContext context, BlockPos pos) {
6262
Object tempState = context.temporaryData;
6363

6464
if (!(tempState instanceof SavedState)) {
65-
tempState = new SavedState(type, null, null, new HashSet<>());
65+
tempState = new SavedState(type, null, null, context.contraption instanceof CarriageContraption, new HashSet<>());
6666
context.temporaryData = tempState;
6767
}
6868

@@ -97,7 +97,7 @@ public void tick(MovementContext context) {
9797
Object tempState = context.temporaryData;
9898

9999
if (!(tempState instanceof SavedState)) {
100-
tempState = new SavedState(type, entityChunkPos, entityBlockPos, new HashSet<>());
100+
tempState = new SavedState(type, entityChunkPos, entityBlockPos, context.contraption instanceof CarriageContraption, new HashSet<>());
101101
context.temporaryData = tempState;
102102

103103
SavedState savedState = (SavedState) tempState;
@@ -166,12 +166,15 @@ static class SavedState implements ChunkLoader {
166166
public LoadedChunkPos chunkPos;
167167
@Nullable
168168
public BlockPos blockPos;
169+
private final boolean isTrain;
169170
public Set<LoadedChunkPos> forcedChunks;
171+
public boolean registered = false;
170172

171-
public SavedState(LoaderType type, @Nullable LoadedChunkPos chunkPos, @Nullable BlockPos blockPos, Set<LoadedChunkPos> forcedChunks) {
173+
public SavedState(LoaderType type, @Nullable LoadedChunkPos chunkPos, @Nullable BlockPos blockPos, boolean isTrain, Set<LoadedChunkPos> forcedChunks) {
172174
loaderType = type;
173175
this.chunkPos = chunkPos;
174176
this.blockPos = blockPos;
177+
this.isTrain = isTrain;
175178
this.forcedChunks = forcedChunks;
176179
addToManager();
177180
}
@@ -191,6 +194,12 @@ public LoaderType getLoaderType() {
191194
return loaderType;
192195
}
193196

197+
@Override
198+
public void addToManager() {
199+
if (!isTrain)
200+
ChunkLoader.super.addToManager();
201+
}
202+
194203
@Override
195204
public @Nullable Pair<ResourceLocation, BlockPos> getLocation() {
196205
if (chunkPos == null || blockPos == null) return null;

src/main/java/com/hlysine/create_power_loader/content/trains/StationChunkLoader.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public class StationChunkLoader implements ChunkLoader {
2929

3030
private final Map<ResourceKey<Level>, Set<LoadedChunkPos>> reclaimedChunks = new HashMap<>();
3131
public final Set<LoadedChunkPos> forcedChunks = new HashSet<>();
32+
private boolean registered = false;
3233

3334

3435
public StationChunkLoader(GlobalStation station) {
3536
this.station = station;
36-
addToManager();
3737
}
3838

3939
@Override
@@ -56,13 +56,25 @@ public LoaderType getLoaderType() {
5656

5757
@Override
5858
public @Nullable Pair<ResourceLocation, BlockPos> getLocation() {
59-
return Pair.of(station.edgeLocation.getFirst().dimension.location(), BlockPos.containing(station.edgeLocation.getFirst().getLocation()));
59+
return Pair.of(
60+
station.edgeLocation.getFirst().dimension.location(),
61+
BlockPos.containing(station.edgeLocation.getFirst().getLocation().add(station.edgeLocation.getSecond().getLocation()).scale(0.5))
62+
);
63+
}
64+
65+
@Override
66+
public void addToManager() {
67+
if (!registered) {
68+
ChunkLoader.super.addToManager();
69+
registered = true;
70+
}
6071
}
6172

6273
public void tick(TrackGraph graph, boolean preTrains) {
6374
if (preTrains) return;
6475
Level level = ChunkLoadManager.tickLevel;
6576
if (level == null || level.isClientSide()) return;
77+
addToManager();
6678

6779
ChunkLoadManager.reclaimChunks(level, station.id, reclaimedChunks);
6880

src/main/java/com/hlysine/create_power_loader/content/trains/TrainChunkLoader.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public class TrainChunkLoader implements ChunkLoader {
2525
private final Train train;
2626
public final List<CarriageChunkLoader> carriageLoaders = new LinkedList<>();
2727
private final Map<ResourceKey<Level>, Set<LoadedChunkPos>> reclaimedChunks = new HashMap<>();
28+
private boolean registered = false;
2829

2930
public TrainChunkLoader(Train train) {
3031
this.train = train;
31-
addToManager();
3232
}
3333

3434
@Override
@@ -64,8 +64,17 @@ public Pair<ResourceLocation, BlockPos> getLocation() {
6464
.orElse(null);
6565
}
6666

67+
@Override
68+
public void addToManager() {
69+
if (!registered) {
70+
ChunkLoader.super.addToManager();
71+
registered = true;
72+
}
73+
}
74+
6775
public void tick(Level level) {
6876
if (level.isClientSide()) return;
77+
addToManager();
6978

7079
// Make sure carriage information is up-to-date
7180
if (carriageLoaders.size() != train.carriages.size()) {

0 commit comments

Comments
 (0)