下面列出了net.minecraft.util.WeightedRandomChestContent#net.minecraft.tileentity.TileEntityChest 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Iterates over the list of loaded tile entities once per tick to find a chest
* Returns a position of the chest if a chest is found with at least one item in it
* @return
*/
public BlockPos findTargetChest()
{
List<TileEntity> list = this.world.loadedTileEntityList;
if (this.target_chest_iter >= list.size())
{
this.target_chest_iter = 0;
}
TileEntity potential_entity = (TileEntity) list.get(this.target_chest_iter);
BlockPos tepos = potential_entity.getPos();
this.target_chest_iter++;
if (potential_entity != null && this.world.getBlockState(tepos).getBlock() == Blocks.CHEST)
{
// TODO maybe put a try/catch here? (life should go on)
TileEntityChest chestent = (TileEntityChest)potential_entity;
for (int i = 0; i < chestent.getSizeInventory(); i++)
{
if (chestent.getStackInSlot(i) != null)
{
return tepos;
}
}
}
return null;
}
@Override
public boolean shouldTrackWithThisEntry(IBlockAccess world, int x, int y, int z, Block block, TileEntity te){
if(tileEntityClassToNameMapping == null) {
try {
tileEntityClassToNameMapping = (Map)ReflectionHelper.findField(TileEntity.class, "field_145853_j", "classToNameMap").get(null);
} catch(Exception e) {
Log.error("[BlockTrackEntryInventory.class] Uhm reflection failed here!");
e.printStackTrace();
}
}
if(te instanceof TileEntityChest) {
TileEntityChest chest = (TileEntityChest)te;
if(chest.adjacentChestXNeg != null || chest.adjacentChestZNeg != null) return false;
}
return te != null && !invBlackList.contains(tileEntityClassToNameMapping.get(te.getClass())) && te instanceof IInventory && ((IInventory)te).getSizeInventory() > 0 && !MinecraftForge.EVENT_BUS.post(new InventoryTrackEvent(te));
}
public ITreasureChest generate(IWorldEditor editor, Random rand, Coord pos, int level, boolean trapped) throws ChestPlacementException {
this.rand = rand;
this.level = level;
MetaBlock chestType = new MetaBlock(trapped ? Blocks.TRAPPED_CHEST : Blocks.CHEST);
boolean success = chestType.set(editor, pos);
if(!success){
throw new ChestPlacementException("Failed to place chest in world");
}
this.chest = (TileEntityChest) editor.getTileEntity(pos);
this.inventory = new Inventory(rand, chest);
this.seed = (long)Objects.hash(pos.hashCode(), editor.getSeed());
editor.addChest(this);
return this;
}
public void finishJob(boolean near)
{
EntityGnomeWood gnomewood = (EntityGnomeWood)this.gnome;
TileEntityGnomeCache cache = (TileEntityGnomeCache) gnomewood.gnode;
if (near && gnomewood.gnode != null)
{
// if this is still a chest
IBlockState state = gnomewood.world.getBlockState(this.pos);
if (state.getBlock() == Blocks.CHEST)
{
TileEntityChest te = (TileEntityChest)gnomewood.world.getTileEntity(this.pos);
if (te.numPlayersUsing == 0)
{
// move contents of chest from chest to gnome
for (int i = 0; i < 27; i++)
{
gnomewood.inventory[i] = te.getStackInSlot(i);
te.setInventorySlotContents(i, ItemStack.EMPTY);
}
if (!gnomewood.world.isRemote)
{
gnomewood.world.setBlockToAir(this.pos);
gnomewood.setCarriedState(state);
}
}
}
cache.resetChestTracker();
}
}
public static boolean isDoubleChest(TileEntity tile) {
int y;
int z;
int[] pos;
int x;
return tile instanceof TileEntityChest && (getTileEntity((x = (pos = getCoords(tile))[0]) - 1, y = pos[1], z = pos[2]) instanceof TileEntityChest || getTileEntity(x + 1, y, z) instanceof TileEntityChest || getTileEntity(x, y, z - 1) instanceof TileEntityChest || getTileEntity(x, y, z + 1) instanceof TileEntityChest);
}
private int getTileEntityColor(TileEntity tileEntity) {
if (tileEntity instanceof TileEntityChest
|| tileEntity instanceof TileEntityDispenser
|| tileEntity instanceof TileEntityShulkerBox) {
return Colors.ORANGE.toBuffer();
} else if (tileEntity instanceof TileEntityEnderChest) {
return Colors.PURPLE.toBuffer();
} else if (tileEntity instanceof TileEntityFurnace) {
return Colors.GRAY.toBuffer();
} else if (tileEntity instanceof TileEntityHopper) {
return Colors.DARK_RED.toBuffer();
} else {
return -1;
}
}
protected void addLootToChest(World world, IBlockState block, BlockPos placementPos) {
if (block.getBlock() == Blocks.CHEST) {
TileEntity tileentity = world.getTileEntity(placementPos);
if (tileentity instanceof TileEntityChest) {
((TileEntityChest) tileentity).setLootTable(LootTableList.CHESTS_END_CITY_TREASURE, world.rand.nextLong());
}
}
}
protected void placeChest(World world, BlockPos placementPos) {
setBlockAndNotifyAdequately(world, placementPos, Blocks.CHEST.getDefaultState());
TileEntity tileentity = world.getTileEntity(placementPos);
if (tileentity instanceof TileEntityChest) {
((TileEntityChest) tileentity).setLootTable(LootTableList.CHESTS_END_CITY_TREASURE, world.rand.nextLong());
}
}
protected void addLootToChest() {
if (block == null) {
return;
}
if (block.getBlock() == Blocks.CHEST) {
TileEntity tileentity = world.getTileEntity(origin.add(x, y, z));
if (tileentity instanceof TileEntityChest) {
((TileEntityChest) tileentity).setLootTable(LootTableList.CHESTS_END_CITY_TREASURE, world.rand.nextLong());
}
}
}
protected boolean canDoDrop(EntityPlayer player, World world, BlockPos pos) {
TileEntity te = world.getTileEntity(pos);
return te != null &&
(
te instanceof IUpgradableBlock //Betterchest
|| te instanceof TileEntityChest //Vanilla chest
|| te.getClass().getCanonicalName().startsWith("cpw.mods.ironchest") //IronChest
);
}
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
if (world.isRemote) {
return EnumActionResult.PASS;
}
IBlockState state = world.getBlockState(pos);
TileEntity te = world.getTileEntity(pos);
if (state.getBlock() == Blocks.CHEST && te instanceof TileEntityChest) {
TileEntityChest chest = (TileEntityChest) te;
ItemStack[] items = new ItemStack[chest.getSizeInventory()];
for (int i = 0; i < items.length; i++) {
items[i] = chest.getStackInSlot(i);
chest.setInventorySlotContents(i, ItemStack.EMPTY);
}
IBlockState newState = BlocksItemsBetterChests.betterchest.getDefaultState().withProperty(BlockBetterChest.directions, state.getValue(BlockChest.FACING));
world.setBlockState(pos, newState, 2);
TileEntityBChest newte = new TileEntityBChest();
world.setTileEntity(pos, newte);
for (int i = 0; i < items.length; i++) {
newte.getChestPart().setInventorySlotContents(i, items[i]);
}
world.notifyBlockUpdate(pos, state, newState, 1);
ItemStack heldItem = player.getHeldItem(hand);
heldItem.setCount(heldItem.getCount() - 1);
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
/**
* Gets an IInventory from a coordinate with support for double chests
*/
public static IInventory getInventory(World world, BlockPos pos) {
TileEntity tile = world.getTileEntity(pos);
if (!(tile instanceof IInventory))
return null;
if (tile instanceof TileEntityChest)
return getChest((TileEntityChest) tile);
return (IInventory) tile;
}
public static IInventory getChest(TileEntityChest chest) {
for (EnumFacing fside : Plane.HORIZONTAL) {
if (chest.getWorld().getBlockState(chest.getPos().offset(fside)).getBlock() == chest.getBlockType())
return new InventoryLargeChest("container.chestDouble",
(TileEntityChest) chest.getWorld().getTileEntity(chest.getPos().offset(fside)), chest);
}
return chest;
}
@Override
public boolean shouldBeUpdatedFromServer(TileEntity te){
if(te instanceof TileEntityChest) {
TileEntityChest chest = (TileEntityChest)te;
if(chest.adjacentChestXPos != null) NetworkHandler.sendToServer(new PacketDescriptionPacketRequest(chest.adjacentChestXPos.xCoord, chest.adjacentChestXPos.yCoord, chest.adjacentChestXPos.zCoord));
if(chest.adjacentChestZPos != null) NetworkHandler.sendToServer(new PacketDescriptionPacketRequest(chest.adjacentChestZPos.xCoord, chest.adjacentChestZPos.yCoord, chest.adjacentChestZPos.zCoord));
}
return true;
}
public Inventory(Random rand, TileEntityChest chest){
this.chest = chest;
this.shuffledSlots = new ArrayList<Integer>();
for(int i = 0; i < this.getInventorySize(); ++i){
shuffledSlots.add(i);
}
Collections.shuffle(shuffledSlots, rand);
}
public CraftChest(final Block block) {
super(block, TileEntityChest.class);
}
public CraftChest(final Material material, final TileEntityChest te) {
super(material, te);
}
protected void addLootToChest(BlockPos placementPos) {
TileEntity tileentity = world.getTileEntity(placementPos);
if (tileentity instanceof TileEntityChest) {
addLootToChest((TileEntityChest) tileentity);
}
}
public CraftChest(final Block block) {
super(block);
world = (CraftWorld) block.getWorld();
chest = (TileEntityChest) world.getTileEntityAt(getX(), getY(), getZ());
}
public MockChest(Treasure type, int level){
this.type = type;
this.level = level;
this.chest = new TileEntityChest();
this.inv = new Inventory(new Random(), chest);
}
@Override
public ResponseMessage onMessage(TileRequest message, MessageContext ctx)
{
World world = DimensionManager.getWorld(message.dim);
if (world == null) return null;
TileEntity te = world.getTileEntity(message.pos);
if (te == null) return null;
if (Helper.banned.contains(te.getClass().getCanonicalName())) return null;
if (te instanceof ILockableContainer && !ctx.getServerHandler().player.canOpen(((ILockableContainer) te).getLockCode())) return null;
if (te instanceof TileEntityEnderChest)
{
return new PlainInventory(message.pos, ctx.getServerHandler().player.getInventoryEnderChest());
}
else if (te instanceof BlockJukebox.TileEntityJukebox)
{
InventoryBasic ib = new InventoryBasic("minecraft:jukebox", false, 1);
ib.setInventorySlotContents(0, ((BlockJukebox.TileEntityJukebox) te).getRecord());
return new PlainInventory(message.pos, ib).setName(Blocks.JUKEBOX.getUnlocalizedName());
}
else if (te instanceof TileEntityChest)
{
Block b = world.getBlockState(message.pos).getBlock();
if (b instanceof BlockChest)
{
IInventory i = ((BlockChest) b).getLockableContainer(world, message.pos);
if (i != null) return new PlainInventory(message.pos, i);
return null;
}
return new PlainInventory(message.pos, ((TileEntityChest) te));
}
else if (te instanceof IInventory)
{
return new PlainInventory(message.pos, (IInventory) te);
}
else if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
{
IItemHandler iih = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (iih == null)
{
HoloInventory.getLogger().warn("Error: Block at {} (Class: {} Te: {} Block: {}) returned null after indicating the capability is available.", message.pos, te.getClass().getName(), te, te.getBlockType());
return null;
}
if (te instanceof INamedItemHandler) {
INamedItemHandler namedHandler = (INamedItemHandler) te;
return new PlainInventory(message.pos, namedHandler.getItemHandlerName(), iih);
}
return new PlainInventory(message.pos, te.getBlockType().getUnlocalizedName(), iih);
}
return null;
}
private static void basement(World world, int i, int j, int k, Random rand) {
boolean noair = false;
int y = -1;
do {
noair = false;
for(int x = 1; x <= 9; x++) {
for(int z = 0; z <= 8; z++) {
//5,y,4
int d = (x-5)*(x-5)+(z-4)*(z-4);
if(d <= 17) {
if(!world.getBlock(i+x, j+y, z+k).isOpaqueCube() || world.getBlock(i+x, j+y, z+k) == Blocks.leaves) {
noair=true;
world.setBlock(i+x, j+y, z+k, StructureGenHelper.cobbleOrMossy(rand), 0, 2);
}
}
}
}
y--;
} while(noair && (j+y) >= 0);
if(y >= -7 && world.rand.nextBoolean()) {
y = -8;
}
if(y < -7) {
y++;
int yy = 3;
for(; yy <= 5; yy++) {
for(int x = 3; x <= 7; x++) {
for(int z = 2; z <= 6; z++) {
world.setBlockToAir(i+x, j+y+yy, k+z);
}
}
}
world.setBlock(i+5, j+y+5, k+4, Blocks.mob_spawner, 0, 2);
TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner)world.getTileEntity(i+5, j+y+5, k+4);
if (tileentitymobspawner != null)
{
tileentitymobspawner.func_145881_a/*getSpawnerLogic*/().setEntityName("ClayGolem");
NBTTagCompound nbt = new NBTTagCompound();
tileentitymobspawner.writeToNBT(nbt);
nbt.setShort("MinSpawnDelay",(short)100);
nbt.setShort("MaxSpawnDelay",(short)600);
tileentitymobspawner.readFromNBT(nbt);
}
world.setBlock(i+5, j+y+4, k+4, StructureGenHelper.randomBlock(rand, new Block[]{Blocks.chest, Blocks.chest, Blocks.air}), 2, 2);
TileEntity te = world.getTileEntity(i+5, j+y+4, k+4);
if(te != null && te instanceof TileEntityChest) {
TileEntityChest tec = (TileEntityChest)te;
ChestGenHooks info = ChestGenHooks.getInfo(ChestGenHooks.DUNGEON_CHEST);
WeightedRandomChestContent.generateChestContents(rand, info.getItems(rand), tec, info.getCount(rand));
}
world.setBlock(i+5, j+y+3, k+4, StructureGenHelper.cobbleOrMossy(rand), 0, 2);
for(yy=3;yy*-1>y;yy++) {
world.setBlock(i+7, j+y+yy, k+6, Blocks.ladder, 2, 2);
}
world.setBlock(i+7, j+y+yy, k+6, Blocks.ladder, 2, 2);
world.setBlock(i+7, j+y+yy+1, k+6, Blocks.trapdoor, 2, 2);
}
}