From 8abc8df5f900106e9d2f7f4bf9ca3c471ca2ecae Mon Sep 17 00:00:00 2001 From: 3pical <93999900+3pical@users.noreply.github.com> Date: Sat, 22 Oct 2022 19:15:13 +0300 Subject: [PATCH] Supplementaries Sack Fix Fixes the Sacks from forgetting their name --- scripts/sack_fix.zs | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 scripts/sack_fix.zs diff --git a/scripts/sack_fix.zs b/scripts/sack_fix.zs new file mode 100644 index 00000000..e452a291 --- /dev/null +++ b/scripts/sack_fix.zs @@ -0,0 +1,81 @@ +import crafttweaker.api.item.IItemStack; +import crafttweaker.api.loot.LootManager; +import crafttweaker.api.loot.conditions.LootConditionBuilder; +import crafttweaker.api.loot.modifiers.CommonLootModifiers; +import crafttweaker.api.loot.LootContext; +import crafttweaker.api.tileentity.MCTileEntity; +import crafttweaker.api.data.MapData; +import crafttweaker.api.data.IData; +import stdlib.List; + + +// A lot of this was written by TheSilkMiner(Silk#6031 on Discord), Huge thanks to them. + +.addLootModifier( + "sack_fix", + (drops, context) => { + + // Grab the tile entity from the context + val maybeBe = context.tileEntity; + + // Check that a tile entity does in fact exist, to avoid crashing in case something is weird + if (maybeBe != null) { + + // And here we tell the game we are sure the tile entity exists + val be = maybeBe as MCTileEntity; + + // Grab the block entity's raw NBT data + val data = be.data; + + // By using /data in-game, we find out that the name is stored in NBT with the key 'CustomName', so we want to grab that + + // Note that this might return null, as it might or might not have a custom name + // Grabs the "CustomName" in the block NBT + val customName = data.getAt("CustomName"); + + // Grabs the "Items" in the block NBT, This will be needed later when converting to handheld item format + val items = data.getAt("Items"); + + // If the block entity has a custom name, then we need to apply it to the item + if (customName != null) { + + // Let's create a new list of drops so that we can manipulate it more easily + val newDrops = new stdlib.List(); + + // Here we want to find the default drop, so we loop over the original set of drops + for stack in drops { + + // If the item is the one we are looking for + if .matches(stack) { + + // We need to construct the new NBT, so we have to do the entire thing for the sack + + val nameData = new MapData(); + nameData.put("Name", customName); + + val itemData = new MapData(); + itemData.put("Items", items); + + val newData = new MapData(); + newData.put("display", nameData); + newData.put("BlockEntityTag", itemData); + + + + // And add it to the list + newDrops.add(stack.withTag(newData)); + } + + // If it's not the item we care, about we add it anyway, without modifications + else { + newDrops.add(stack); + } + } + // Finally, let's tell the game it should use the new drops we made up + return newDrops; + } + } + // If the block didn't have a tile entity or it had no custom name, we just return the old drops as they're fine as they are + return drops; + } +); \ No newline at end of file