Hello so I have a hunger games map and a custom loot table I want to use to fill my chests. I want to be able to set these chests to a custom loot table on command. I have looked into the /fill command but I run into two limitations – orientations are not preserved upon replacing the old chests, and two that the arena is too large to load all at once so the command fails to work. Is there any solution to this? Preferably I would be able to fill the chests seamlessly and repeatedly so that I can easily reset the map for further use.
Minecraft – How to Replace All Chests with Ones with a Loot Table
minecraft-java-editionminecraft-java-edition-server
Related Topic
- Minecraft: How to make a command block shop that allows players to sell items
- Minecraft – Restrict access to certain actions in certain areas
- Minecraft – How to handle arena equipment in 1.7.10 so it can’t be taken away by players
- Minecraft – /blockdata ~ ~ ~ {Items:[]} does not work at a certain distance away from the player
- Minecraft – Help with datapack/loot table commands in 1.13
- Minecraft – In Minecraft, how can I merge info from two map items while exploring
Best Answer
I'm currently working on something similar, and the best solution I've found so far is the following:
Use Datapacks with functions, not command block chains, that saves tons of performance
you have to create separate
setblock
functions for every kind of chests you have, multiply by four for orientation (six for barrels). You can separate spawning and filling usingdata merge
, but you can't change block state of existing block, at least in vanilla, so you have to create a function for each orientation at least. The former is probably possible using WorldEdit since it exposes "cycler" tool which does exactly that.Your containers should be put at set places, since, to the best of my knowledge, you can't use a type selector for blocks. Unless you don't want to go crazy with spawning armor stands and testing for each block in the area, you should rather use
execute positioned <pos> as entity @p[distance=..R]
functions. You can use the chest position itself aspos
, thenR
is the spawn trigger distance, which I set to the very possible minimum of 4 (as the chest is reachable from within the range of 3), and it's adjustable. Another option is to setpos
to some enclosed area entrance (whereR
is the entrance size basically), which is impossible to bypass, and then trigger chest creation by another layer ofexecute positioned
. Those checks should be running every tick, which you achieve either by using repeating command block or by putting your function into#minecraft:tick
function tag.To prevent repetitive spawning you want to use Tags. That's a little bit verbose, but pretty efficient. Depending on your goals you can tag either a player or the chest at the moment it's filled. So it can be filled once per lifetime (totally multiplayer safe), once per every player approaching (not so much multiplayer safe), or, if you're crazy enough with state management, you can force a chest respawn once for every player but only if it's emptied. To do so, you should tag both a player and a chest on spawn event, and create another
tick
command which tracks previously tagged chests and removes that tag, so the state is fully reset for the chest (but not for the player). If you need too many tags, you could use code generation.Group up chests spawn whenever you can. For instance, if there's a room with 2 entrances and 15 chests, it's better to test for player presence in each of entrances rather than to do so around every chest. Whenever possible try to use redstone wiring (pressure plates, trapped chests, tripwires etc. ) with impulse command blocks to test for player approach rather then
tick
function, for performance reasons.So the example code would be like
I have more examples here if you need them https://gitlab.com/octaharon/minecraft-quest/-/tree/master/data/chests