Skip to content

Commit 787a4bb

Browse files
committed
Test selector for clear.
1 parent e289958 commit 787a4bb

File tree

2 files changed

+103
-68
lines changed

2 files changed

+103
-68
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ org.gradle.daemon=false
44
org.gradle.jvmargs=-Xmx6500m
55

66
group=noobanidus.mods.lootr
7-
version=0.7.35.90
7+
version=0.7.35.91
88
archive_name=lootr-forge-1.20
99

1010
mod_name=Lootr

src/main/java/noobanidus/mods/lootr/command/CommandLootr.java

Lines changed: 102 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.commands.CommandSourceStack;
1111
import net.minecraft.commands.Commands;
1212
import net.minecraft.commands.SharedSuggestionProvider;
13+
import net.minecraft.commands.arguments.EntityArgument;
1314
import net.minecraft.commands.arguments.ResourceLocationArgument;
1415
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
1516
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
@@ -24,6 +25,7 @@
2425
import net.minecraft.server.level.ServerLevel;
2526
import net.minecraft.server.level.ServerPlayer;
2627
import net.minecraft.world.entity.Entity;
28+
import net.minecraft.world.entity.player.Player;
2729
import net.minecraft.world.item.ItemStack;
2830
import net.minecraft.world.level.ChunkPos;
2931
import net.minecraft.world.level.Level;
@@ -107,7 +109,9 @@ public static void createBlock(CommandSourceStack c, @Nullable Block block, @Nul
107109
}
108110
cart.setLootTable(table, world.getRandom().nextLong());
109111
world.addFreshEntity(cart);
110-
c.sendSuccess(() -> Component.translatable("lootr.commands.summon", ComponentUtils.wrapInSquareBrackets(Component.translatable("lootr.commands.blockpos", pos.getX(), pos.getY(), pos.getZ()).setStyle(Style.EMPTY.withColor(TextColor.fromLegacyFormat(ChatFormatting.GREEN)).withBold(true))), table.toString()), false);
112+
c.sendSuccess(() -> Component.translatable("lootr.commands.summon", ComponentUtils.wrapInSquareBrackets(Component.translatable("lootr.commands.blockpos", pos.getX(), pos.getY(), pos.getZ())
113+
.setStyle(Style.EMPTY.withColor(TextColor.fromLegacyFormat(ChatFormatting.GREEN))
114+
.withBold(true))), table.toString()), false);
111115
} else {
112116
BlockState placementState = block.defaultBlockState();
113117
Entity e = c.getEntity();
@@ -128,7 +132,9 @@ public static void createBlock(CommandSourceStack c, @Nullable Block block, @Nul
128132
}
129133
world.setBlock(pos, placementState, 2);
130134
RandomizableContainerBlockEntity.setLootTable(world, world.getRandom(), pos, table);
131-
c.sendSuccess(() -> Component.translatable("lootr.commands.create", Component.translatable(block.getDescriptionId()), ComponentUtils.wrapInSquareBrackets(Component.translatable("lootr.commands.blockpos", pos.getX(), pos.getY(), pos.getZ()).setStyle(Style.EMPTY.withColor(TextColor.fromLegacyFormat(ChatFormatting.GREEN)).withBold(true))), table.toString()), false);
135+
c.sendSuccess(() -> Component.translatable("lootr.commands.create", Component.translatable(block.getDescriptionId()), ComponentUtils.wrapInSquareBrackets(Component.translatable("lootr.commands.blockpos", pos.getX(), pos.getY(), pos.getZ())
136+
.setStyle(Style.EMPTY.withColor(TextColor.fromLegacyFormat(ChatFormatting.GREEN))
137+
.withBold(true))), table.toString()), false);
132138
}
133139
}
134140

@@ -143,7 +149,8 @@ private RequiredArgumentBuilder<CommandSourceStack, ResourceLocation> suggestTab
143149
}
144150

145151
private RequiredArgumentBuilder<CommandSourceStack, String> suggestProfiles() {
146-
return Commands.argument("profile", StringArgumentType.string()).suggests((c, build) -> SharedSuggestionProvider.suggest(getProfiles(), build));
152+
return Commands.argument("profile", StringArgumentType.string())
153+
.suggests((c, build) -> SharedSuggestionProvider.suggest(getProfiles(), build));
147154
}
148155

149156
public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder<CommandSourceStack> builder) {
@@ -186,13 +193,28 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
186193
String playerName = StringArgumentType.getString(c, "profile");
187194
Optional<GameProfile> opt_profile = c.getSource().getServer().getProfileCache().get(playerName);
188195
if (!opt_profile.isPresent()) {
189-
c.getSource().sendFailure(Component.literal("Invalid player name: " + playerName + ", profile not found in the cache."));
196+
c.getSource()
197+
.sendFailure(Component.literal("Invalid player name: " + playerName + ", profile not found in the cache."));
190198
return 0;
191199
}
192200
GameProfile profile = opt_profile.get();
193-
c.getSource().sendSuccess(() -> Component.literal(DataStorage.clearInventories(profile.getId()) ? "Cleared stored inventories for " + playerName : "No stored inventories for " + playerName + " to clear"), true);
201+
c.getSource()
202+
.sendSuccess(() -> Component.literal(DataStorage.clearInventories(profile.getId()) ? "Cleared stored inventories for " + playerName : "No stored inventories for " + playerName + " to clear"), true);
194203
return 1;
195204
})));
205+
206+
builder.then(Commands.literal("cclear")
207+
.then(Commands.argument("entities", EntityArgument.entities()).executes(c -> {
208+
Collection<? extends Entity> entities = EntityArgument.getEntities(c, "entities");
209+
for (Entity e : entities) {
210+
if (e instanceof Player player) {
211+
c.getSource()
212+
.sendSuccess(() -> Component.literal(DataStorage.clearInventories(player.getUUID()) ? "Cleared stored inventories for " + player.getName() : "No stored inventories for " + player.getName() + " to clear"), true);
213+
}
214+
}
215+
216+
return 1;
217+
})));
196218
builder.then(Commands.literal("cart").executes(c -> {
197219
createBlock(c.getSource(), null, null);
198220
return 1;
@@ -209,13 +231,16 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
209231
state = world.getBlockState(pos);
210232
}
211233
if (!state.is(Blocks.CHEST) && !state.is(Blocks.BARREL)) {
212-
c.getSource().sendSuccess(() -> Component.literal("Please stand on the chest or barrel you wish to convert."), false);
234+
c.getSource()
235+
.sendSuccess(() -> Component.literal("Please stand on the chest or barrel you wish to convert."), false);
213236
} else {
214237
NonNullList<ItemStack> reference;
215238
BlockState newState;
216239
if (state.is(Blocks.CHEST)) {
217240
reference = ((ChestBlockEntity) Objects.requireNonNull(world.getBlockEntity(pos))).items;
218-
newState = ModBlocks.INVENTORY.get().defaultBlockState().setValue(ChestBlock.FACING, state.getValue(ChestBlock.FACING)).setValue(ChestBlock.WATERLOGGED, state.getValue(ChestBlock.WATERLOGGED));
241+
newState = ModBlocks.INVENTORY.get().defaultBlockState()
242+
.setValue(ChestBlock.FACING, state.getValue(ChestBlock.FACING))
243+
.setValue(ChestBlock.WATERLOGGED, state.getValue(ChestBlock.WATERLOGGED));
219244
} else {
220245
Direction facing = state.getValue(BarrelBlock.FACING);
221246
if (facing == Direction.UP || facing == Direction.DOWN) {
@@ -229,7 +254,8 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
229254
world.setBlockAndUpdate(pos, newState);
230255
BlockEntity te = world.getBlockEntity(pos);
231256
if (!(te instanceof LootrInventoryBlockEntity inventory)) {
232-
c.getSource().sendSuccess(() -> Component.literal("Unable to convert chest, BlockState is not a Lootr Inventory block."), false);
257+
c.getSource()
258+
.sendSuccess(() -> Component.literal("Unable to convert chest, BlockState is not a Lootr Inventory block."), false);
233259
} else {
234260
inventory.setCustomInventory(custom);
235261
inventory.setChanged();
@@ -244,7 +270,8 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
244270
String playerName = StringArgumentType.getString(c, "profile");
245271
Optional<GameProfile> opt_profile = c.getSource().getServer().getProfileCache().get(playerName);
246272
if (!opt_profile.isPresent()) {
247-
c.getSource().sendFailure(Component.literal("Invalid player name: " + playerName + ", profile not found in the cache."));
273+
c.getSource()
274+
.sendFailure(Component.literal("Invalid player name: " + playerName + ", profile not found in the cache."));
248275
return 0;
249276
}
250277
GameProfile profile = opt_profile.get();
@@ -329,7 +356,8 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
329356
if (!(te instanceof ILootBlockEntity ibe)) {
330357
c.getSource().sendSuccess(() -> Component.literal("Please stand on a valid Lootr container."), false);
331358
} else {
332-
c.getSource().sendSuccess(() -> Component.literal("The ID of this inventory is: ").append(ComponentUtils.copyOnClickText(ibe.getTileId().toString())), false);
359+
c.getSource().sendSuccess(() -> Component.literal("The ID of this inventory is: ")
360+
.append(ComponentUtils.copyOnClickText(ibe.getTileId().toString())), false);
333361
}
334362
return 1;
335363
}));
@@ -343,7 +371,8 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
343371
}
344372
if (be instanceof ILootBlockEntity ibe) {
345373
DataStorage.setRefreshing(((ILootBlockEntity) be).getTileId(), ConfigManager.REFRESH_VALUE.get());
346-
c.getSource().sendSuccess(() -> Component.literal("Container with ID " + (ibe).getTileId() + " has been set to refresh with a delay of " + ConfigManager.REFRESH_VALUE.get()), false);
374+
c.getSource()
375+
.sendSuccess(() -> Component.literal("Container with ID " + (ibe).getTileId() + " has been set to refresh with a delay of " + ConfigManager.REFRESH_VALUE.get()), false);
347376
} else {
348377
c.getSource().sendSuccess(() -> Component.literal("Please stand on a valid Lootr container."), false);
349378
}
@@ -359,7 +388,8 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
359388
}
360389
if (be instanceof ILootBlockEntity ibe) {
361390
DataStorage.setDecaying((ibe).getTileId(), ConfigManager.DECAY_VALUE.get());
362-
c.getSource().sendSuccess(() -> Component.literal("Container with ID " + (ibe).getTileId() + " has been set to decay with a delay of " + ConfigManager.DECAY_VALUE.get()), false);
391+
c.getSource()
392+
.sendSuccess(() -> Component.literal("Container with ID " + (ibe).getTileId() + " has been set to decay with a delay of " + ConfigManager.DECAY_VALUE.get()), false);
363393
} else {
364394
c.getSource().sendSuccess(() -> Component.literal("Please stand on a valid Lootr container."), false);
365395
}
@@ -371,74 +401,79 @@ public LiteralArgumentBuilder<CommandSourceStack> builder(LiteralArgumentBuilder
371401
BlockEntity tile = world.getBlockEntity(position);
372402
if (tile instanceof ILootBlockEntity ibe) {
373403
Set<UUID> openers = ((ILootBlockEntity) tile).getOpeners();
374-
c.getSource().sendSuccess(() -> Component.literal("Tile at location " + position + " has " + openers.size() + " openers. UUIDs as follows:"), true);
404+
c.getSource()
405+
.sendSuccess(() -> Component.literal("Tile at location " + position + " has " + openers.size() + " openers. UUIDs as follows:"), true);
375406
for (UUID uuid : openers) {
376407
Optional<GameProfile> prof = c.getSource().getServer().getProfileCache().get(uuid);
377-
c.getSource().sendSuccess(() -> Component.literal("UUID: " + uuid.toString() + ", user profile: " + (prof.isPresent() ? prof.get().getName() : "null")), true);
408+
c.getSource()
409+
.sendSuccess(() -> Component.literal("UUID: " + uuid.toString() + ", user profile: " + (prof.isPresent() ? prof.get()
410+
.getName() : "null")), true);
378411
}
379412
} else {
380413
c.getSource().sendSuccess(() -> Component.literal("No Lootr tile exists at location: " + position), false);
381414
}
382415
return 1;
383416
})));
384-
builder.then(Commands.literal("convert").then(Commands.argument("from", BlockPosArgument.blockPos()).then(Commands.argument("to", BlockPosArgument.blockPos()).executes(context -> {
385-
BoundingBox bounds = BoundingBox.fromCorners(BlockPosArgument.getLoadedBlockPos(context, "from"), BlockPosArgument.getLoadedBlockPos(context, "to"));
386-
ChunkPos start = new ChunkPos(new BlockPos(bounds.minX(), bounds.minY(), bounds.minZ()));
387-
ChunkPos stop = new ChunkPos(new BlockPos(bounds.maxX(), bounds.maxY(), bounds.maxZ()));
388-
List<ChunkPos> positions = new ArrayList<>();
389-
for (int x = start.x; x <= stop.x; x++) {
390-
for (int z = start.z; z <= stop.z; z++) {
391-
positions.add(new ChunkPos(x, z));
392-
}
393-
}
394-
ServerLevel level = context.getSource().getLevel();
395-
for (ChunkPos chunkPos : positions) {
396-
LevelChunk chunk = level.getChunk(chunkPos.x, chunkPos.z);
397-
List<BlockPos> convertableBlocks = new ArrayList<>();
398-
for (BlockPos pos : chunk.getBlockEntitiesPos()) {
399-
if (!bounds.isInside(pos)) {
400-
continue;
401-
}
402-
convertableBlocks.add(pos);
403-
}
404-
if (convertableBlocks.isEmpty()) {
405-
continue;
406-
}
407-
for (BlockPos pos : convertableBlocks) {
408-
BlockEntity blockEntity = chunk.getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
409-
if (!(blockEntity instanceof BaseContainerBlockEntity) || blockEntity instanceof ILootBlockEntity) {
410-
continue;
417+
builder.then(Commands.literal("convert").then(Commands.argument("from", BlockPosArgument.blockPos())
418+
.then(Commands.argument("to", BlockPosArgument.blockPos()).executes(context -> {
419+
BoundingBox bounds = BoundingBox.fromCorners(BlockPosArgument.getLoadedBlockPos(context, "from"), BlockPosArgument.getLoadedBlockPos(context, "to"));
420+
ChunkPos start = new ChunkPos(new BlockPos(bounds.minX(), bounds.minY(), bounds.minZ()));
421+
ChunkPos stop = new ChunkPos(new BlockPos(bounds.maxX(), bounds.maxY(), bounds.maxZ()));
422+
List<ChunkPos> positions = new ArrayList<>();
423+
for (int x = start.x; x <= stop.x; x++) {
424+
for (int z = start.z; z <= stop.z; z++) {
425+
positions.add(new ChunkPos(x, z));
426+
}
411427
}
412-
if (blockEntity instanceof RandomizableContainerBlockEntity lootContainer) {
413-
if (lootContainer.lootTable != null) {
428+
ServerLevel level = context.getSource().getLevel();
429+
for (ChunkPos chunkPos : positions) {
430+
LevelChunk chunk = level.getChunk(chunkPos.x, chunkPos.z);
431+
List<BlockPos> convertableBlocks = new ArrayList<>();
432+
for (BlockPos pos : chunk.getBlockEntitiesPos()) {
433+
if (!bounds.isInside(pos)) {
434+
continue;
435+
}
436+
convertableBlocks.add(pos);
437+
}
438+
if (convertableBlocks.isEmpty()) {
414439
continue;
415440
}
441+
for (BlockPos pos : convertableBlocks) {
442+
BlockEntity blockEntity = chunk.getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
443+
if (!(blockEntity instanceof BaseContainerBlockEntity) || blockEntity instanceof ILootBlockEntity) {
444+
continue;
445+
}
446+
if (blockEntity instanceof RandomizableContainerBlockEntity lootContainer) {
447+
if (lootContainer.lootTable != null) {
448+
continue;
449+
}
450+
}
451+
BlockState state = blockEntity.getBlockState();
452+
NonNullList<ItemStack> reference;
453+
if (blockEntity instanceof BarrelBlockEntity barrelBlock) {
454+
reference = barrelBlock.items;
455+
} else if (blockEntity instanceof ChestBlockEntity chestBlock) {
456+
reference = chestBlock.items;
457+
} else {
458+
continue;
459+
}
460+
BlockState newState = updateBlockState(state, ModBlocks.INVENTORY.get().defaultBlockState());
461+
NonNullList<ItemStack> custom = copyItemList(reference);
462+
level.removeBlockEntity(pos);
463+
level.setBlockAndUpdate(pos, newState);
464+
BlockEntity te = level.getBlockEntity(pos);
465+
if (!(te instanceof LootrInventoryBlockEntity inventory)) {
466+
context.getSource()
467+
.sendSuccess(() -> Component.literal("Unable to convert chest, BlockState is not a Lootr Inventory block."), false);
468+
} else {
469+
inventory.setCustomInventory(custom);
470+
inventory.setChanged();
471+
}
472+
}
416473
}
417-
BlockState state = blockEntity.getBlockState();
418-
NonNullList<ItemStack> reference;
419-
if (blockEntity instanceof BarrelBlockEntity barrelBlock) {
420-
reference = barrelBlock.items;
421-
} else if (blockEntity instanceof ChestBlockEntity chestBlock) {
422-
reference = chestBlock.items;
423-
} else {
424-
continue;
425-
}
426-
BlockState newState = updateBlockState(state, ModBlocks.INVENTORY.get().defaultBlockState());
427-
NonNullList<ItemStack> custom = copyItemList(reference);
428-
level.removeBlockEntity(pos);
429-
level.setBlockAndUpdate(pos, newState);
430-
BlockEntity te = level.getBlockEntity(pos);
431-
if (!(te instanceof LootrInventoryBlockEntity inventory)) {
432-
context.getSource().sendSuccess(() -> Component.literal("Unable to convert chest, BlockState is not a Lootr Inventory block."), false);
433-
} else {
434-
inventory.setCustomInventory(custom);
435-
inventory.setChanged();
436-
}
437-
}
438-
}
439474

440-
return 1;
441-
}))));
475+
return 1;
476+
}))));
442477
return builder;
443478
}
444479

0 commit comments

Comments
 (0)