diff --git a/src/main/java/com/BrassAmber/ba_bt/entity/block/BTAbstractObelisk.java b/src/main/java/com/BrassAmber/ba_bt/entity/block/BTAbstractObelisk.java index f455e4fb..53cf778e 100644 --- a/src/main/java/com/BrassAmber/ba_bt/entity/block/BTAbstractObelisk.java +++ b/src/main/java/com/BrassAmber/ba_bt/entity/block/BTAbstractObelisk.java @@ -442,7 +442,7 @@ private void checkSpawners(Level level) { chest.setUnlocked(true); LootContext.Builder lootcontext$builder = (new LootContext.Builder((ServerLevel)this.level)).withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(chestPos)).withOptionalRandomSeed(this.random.nextLong()); assert chest != null: "BTObelisk: Not a BTChest"; - getLootTable(GolemType.getNumForType(this.golemType), i).fill(chest, lootcontext$builder.create(LootContextParamSets.CHEST)); + BTUtil.btFill(getLootTable(GolemType.getNumForType(this.golemType), i), chest, lootcontext$builder.create(LootContextParamSets.CHEST)); this.chestUnlockingSound(level); this.CHESTS.set(i, null); } diff --git a/src/main/java/com/BrassAmber/ba_bt/util/BTUtil.java b/src/main/java/com/BrassAmber/ba_bt/util/BTUtil.java index f536a62c..b770e1c0 100644 --- a/src/main/java/com/BrassAmber/ba_bt/util/BTUtil.java +++ b/src/main/java/com/BrassAmber/ba_bt/util/BTUtil.java @@ -1,17 +1,19 @@ package com.BrassAmber.ba_bt.util; -import com.BrassAmber.ba_bt.block.blockentity.GolemChestBlockEntity; -import com.BrassAmber.ba_bt.block.blockentity.TowerChestBlockEntity; import com.BrassAmber.ba_bt.init.BTBlockEntityTypes; import com.BrassAmber.ba_bt.init.BTBlocks; import com.BrassAmber.ba_bt.sound.BTSoundEvents; +import com.google.common.collect.Lists; +import com.mojang.logging.LogUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; import net.minecraft.util.Mth; +import net.minecraft.world.Container; import net.minecraft.world.entity.Entity; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.Level; @@ -19,13 +21,17 @@ import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.ChestBlockEntity; +import net.minecraft.world.level.storage.loot.LootContext; +import net.minecraft.world.level.storage.loot.LootTable; import net.minecraft.world.phys.Vec3; import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; import java.util.*; public class BTUtil { + static final Logger LOGGER = LogUtils.getLogger(); public static java.util.function.Supplier getTowerMusic(GolemType type) { return switch (type) { @@ -179,6 +185,79 @@ public static void removeBodyOWater(Set storage, BlockPos position, in removeBodyOWater(storage, position.below(), recursion + 1, level); } + /** All below are recreations of @LootTable methods. + * Needed Due to bukkit servers overwriting the fill method and breaking it. + */ + public static void btFill(LootTable loot, Container p_79124_, LootContext p_79125_) { + List list = loot.getRandomItems(p_79125_); + Random random = p_79125_.getRandom(); + List list1 = btGetAvailableSlots(p_79124_, random); + btShuffleAndSplitItems(list, list1.size(), random); + + for(ItemStack itemstack : list) { + if (list1.isEmpty()) { + LOGGER.warn("Tried to over-fill a container"); + return; + } + + if (itemstack.isEmpty()) { + p_79124_.setItem(list1.remove(list1.size() - 1), ItemStack.EMPTY); + } else { + p_79124_.setItem(list1.remove(list1.size() - 1), itemstack); + } + } + + } + + private static List btGetAvailableSlots(Container p_79127_, Random p_79128_) { + List list = Lists.newArrayList(); + + for(int i = 0; i < p_79127_.getContainerSize(); ++i) { + if (p_79127_.getItem(i).isEmpty()) { + list.add(i); + } + } + + Collections.shuffle(list, p_79128_); + return list; + } + + + private static void btShuffleAndSplitItems(List itemStackList, int listSize, Random random) { + List list = Lists.newArrayList(); + Iterator iterator = itemStackList.iterator(); + + while(iterator.hasNext()) { + ItemStack itemstack = iterator.next(); + if (itemstack.isEmpty()) { + iterator.remove(); + } else if (itemstack.getCount() > 1) { + list.add(itemstack); + iterator.remove(); + } + } + + while(listSize - itemStackList.size() - list.size() > 0 && !list.isEmpty()) { + ItemStack itemstack2 = list.remove(Mth.nextInt(random, 0, list.size() - 1)); + int i = Mth.nextInt(random, 1, itemstack2.getCount() / 2); + ItemStack itemstack1 = itemstack2.split(i); + if (itemstack2.getCount() > 1 && random.nextBoolean()) { + list.add(itemstack2); + } else { + itemStackList.add(itemstack2); + } + + if (itemstack1.getCount() > 1 && random.nextBoolean()) { + list.add(itemstack1); + } else { + itemStackList.add(itemstack1); + } + } + + itemStackList.addAll(list); + Collections.shuffle(itemStackList, random); + } + public static void doCommand(Entity self, String command) { self.level.getServer().getCommands().performCommand(self.createCommandSourceStack().withPermission(4), command); }