From b39ee8228da695755827b9ba798085ce0ea94591 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 20 Nov 2016 13:22:36 +0100 Subject: [PATCH] Replace chests in village houses and lush caves with storage crates. Also closes #382 --- .../mod/blocks/BlockGiantChest.java | 1 + .../mod/gen/VillageComponentJamHouse.java | 25 +++++++++- .../mod/gen/WorldGenLushCaves.java | 26 +++++----- .../mod/tile/TileEntityGiantChest.java | 48 ++++++++++++++++++- 4 files changed, 86 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockGiantChest.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockGiantChest.java index 8478fa859..c4506411b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockGiantChest.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockGiantChest.java @@ -83,6 +83,7 @@ public class BlockGiantChest extends BlockContainerBase{ if(!world.isRemote){ TileEntityGiantChest chest = (TileEntityGiantChest)world.getTileEntity(pos); if(chest != null){ + chest.fillWithLoot(player); player.openGui(ActuallyAdditions.instance, GuiHandler.GuiTypes.GIANT_CHEST.ordinal(), world, pos.getX(), pos.getY(), pos.getZ()); } return true; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/gen/VillageComponentJamHouse.java b/src/main/java/de/ellpeck/actuallyadditions/mod/gen/VillageComponentJamHouse.java index dd17c00bb..e48530775 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/gen/VillageComponentJamHouse.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/gen/VillageComponentJamHouse.java @@ -10,11 +10,16 @@ package de.ellpeck.actuallyadditions.mod.gen; +import de.ellpeck.actuallyadditions.mod.blocks.InitBlocks; import de.ellpeck.actuallyadditions.mod.misc.DungeonLoot; +import de.ellpeck.actuallyadditions.mod.tile.TileEntityGiantChest; import net.minecraft.block.*; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.gen.structure.StructureBoundingBox; import net.minecraft.world.gen.structure.StructureComponent; @@ -184,7 +189,7 @@ public class VillageComponentJamHouse extends StructureVillagePieces.House1{ this.fillWithBlocks(world, sbb, 3, 1, 4, 4, 1, 6, Blocks.CARPET.getStateFromMeta(10), Blocks.CARPET.getStateFromMeta(10), false); //Loot Chest - this.generateChest(world, this.boundingBox, rand, 8, 1, 6, DungeonLoot.JAM_HOUSE); + this.generateCrate(world, this.boundingBox, 8, 1, 6, DungeonLoot.JAM_HOUSE); //Torches this.setBlockState(world, Blocks.TORCH.getDefaultState().withProperty(BlockTorch.FACING, EnumFacing.SOUTH), 6, 2, 0, sbb); @@ -201,4 +206,22 @@ public class VillageComponentJamHouse extends StructureVillagePieces.House1{ protected VillagerProfession chooseForgeProfession(int count, VillagerProfession prof){ return InitVillager.jamProfession; } + + protected boolean generateCrate(World world, StructureBoundingBox box, int x, int y, int z, ResourceLocation loot){ + BlockPos pos = new BlockPos(this.getXWithOffset(x, z), this.getYWithOffset(y), this.getZWithOffset(x, z)); + + if(box.isVecInside(pos) && (world.getBlockState(pos).getBlock() != InitBlocks.blockGiantChest)){ + world.setBlockState(pos, InitBlocks.blockGiantChest.getDefaultState(), 2); + + TileEntity tile = world.getTileEntity(pos); + if(tile instanceof TileEntityGiantChest){ + ((TileEntityGiantChest)tile).lootTable = loot; + } + + return true; + } + else{ + return false; + } + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/gen/WorldGenLushCaves.java b/src/main/java/de/ellpeck/actuallyadditions/mod/gen/WorldGenLushCaves.java index 1ab7ce863..b926990e0 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/gen/WorldGenLushCaves.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/gen/WorldGenLushCaves.java @@ -10,7 +10,9 @@ package de.ellpeck.actuallyadditions.mod.gen; +import de.ellpeck.actuallyadditions.mod.blocks.InitBlocks; import de.ellpeck.actuallyadditions.mod.misc.DungeonLoot; +import de.ellpeck.actuallyadditions.mod.tile.TileEntityGiantChest; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; @@ -70,13 +72,13 @@ public class WorldGenLushCaves{ } if(!possiblePoses.isEmpty()){ - boolean chestGenDone = false; + boolean crateGenDone = false; for(int i = 0; i <= amount; i++){ Collections.shuffle(possiblePoses); BlockPos pos = possiblePoses.get(0); if(rand.nextBoolean()){ - boolean genChest = false; + boolean genCrate = false; WorldGenAbstractTree trees; if(rand.nextBoolean()){ @@ -85,7 +87,7 @@ public class WorldGenLushCaves{ } else{ trees = new WorldGenShrub(Blocks.LOG.getDefaultState(), Blocks.LEAVES.getDefaultState()); - genChest = true; + genCrate = true; } } else{ @@ -93,20 +95,20 @@ public class WorldGenLushCaves{ } trees.generate(world, rand, pos.up()); - if(!chestGenDone && genChest){ - BlockPos chestPos = pos.add(MathHelper.getRandomIntegerInRange(rand, -2, 2), MathHelper.getRandomIntegerInRange(rand, 3, 8), MathHelper.getRandomIntegerInRange(rand, -2, 2)); + if(!crateGenDone && genCrate){ + BlockPos cratePos = pos.add(MathHelper.getRandomIntegerInRange(rand, -2, 2), MathHelper.getRandomIntegerInRange(rand, 3, 8), MathHelper.getRandomIntegerInRange(rand, -2, 2)); - IBlockState state = world.getBlockState(chestPos); - if(state != null && state.getBlock().isLeaves(state, world, chestPos)){ - world.setBlockState(chestPos, Blocks.CHEST.getDefaultState()); + IBlockState state = world.getBlockState(cratePos); + if(state != null && state.getBlock().isLeaves(state, world, cratePos)){ + world.setBlockState(cratePos, InitBlocks.blockGiantChest.getDefaultState()); - TileEntity chest = world.getTileEntity(chestPos); - if(chest instanceof TileEntityChest){ - ((TileEntityChest)chest).setLootTable(DungeonLoot.LUSH_CAVES, rand.nextLong()); + TileEntity tile = world.getTileEntity(cratePos); + if(tile instanceof TileEntityGiantChest){ + ((TileEntityGiantChest)tile).lootTable = DungeonLoot.LUSH_CAVES; } } - chestGenDone = true; + crateGenDone = true; } } else{ diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGiantChest.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGiantChest.java index 3f39adf7e..0059bb127 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGiantChest.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGiantChest.java @@ -16,9 +16,19 @@ import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.WorldServer; +import net.minecraft.world.storage.loot.ILootContainer; +import net.minecraft.world.storage.loot.LootContext; +import net.minecraft.world.storage.loot.LootTable; -public class TileEntityGiantChest extends TileEntityInventoryBase implements IButtonReactor{ +import java.util.Random; + +public class TileEntityGiantChest extends TileEntityInventoryBase implements IButtonReactor, ILootContainer{ + + public ResourceLocation lootTable; public TileEntityGiantChest(int slotAmount, String name){ super(slotAmount, name); @@ -28,6 +38,24 @@ public class TileEntityGiantChest extends TileEntityInventoryBase implements IBu this(9*13, "giantChest"); } + @Override + public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + super.writeSyncableNBT(compound, type); + + if(this.lootTable != null){ + compound.setString("LootTable", this.lootTable.toString()); + } + } + + @Override + public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + super.readSyncableNBT(compound, type); + + if(compound.hasKey("LootTable")){ + this.lootTable = new ResourceLocation(compound.getString("LootTable")); + } + } + @Override public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){ return this.isItemValidForSlot(slot, stack); @@ -61,4 +89,22 @@ public class TileEntityGiantChest extends TileEntityInventoryBase implements IBu player.openGui(ActuallyAdditions.instance, type.ordinal(), this.worldObj, this.pos.getX(), this.pos.getY(), this.pos.getZ()); } } + + @Override + public ResourceLocation getLootTable(){ + return this.lootTable; + } + + public void fillWithLoot(EntityPlayer player){ + if(this.lootTable != null && !this.worldObj.isRemote && this.worldObj instanceof WorldServer){ + LootTable table = this.worldObj.getLootTableManager().getLootTableFromLocation(this.lootTable); + this.lootTable = null; + + LootContext.Builder builder = new LootContext.Builder((WorldServer)this.worldObj); + if(player != null){ + builder.withLuck(player.getLuck()); + } + table.fillInventory(this, new Random(), builder.build()); + } + } }