- 40
- 4
- 4
Есть блок с GUI, вопрос как генерировать в нём лут каждые 5 минут, если он пустой.
Я понимаю, что нужно проверять пустой ли он чтобы сгенерировать, но как именно создавать лут из (к примеру) кастомного LootTable и как реализовать таймер в TileEntity?
Я понимаю, что нужно проверять пустой ли он чтобы сгенерировать, но как именно создавать лут из (к примеру) кастомного LootTable и как реализовать таймер в TileEntity?
TileEntityTrashBinSmall.java если нужен:
public class TileEntityTrashBinSmall extends TileEntityLockableLoot implements ITickable {
private NonNullList<ItemStack> chestContents = NonNullList.<ItemStack>withSize(72, ItemStack.EMPTY);
public int numPlayersUsing, ticksSinceSync;
public float lidAngle, prevLidAngle;
@Override
public int getSizeInventory() {
return 72;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isEmpty() {
for (ItemStack stack : this.chestContents) {
if (!stack.isEmpty()) return false;
}
return true;
}
@Override
public String getName() {
return this.hasCustomName() ? this.customName : "container.trash_bin_small";
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.chestContents = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
if (!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, chestContents);
if (compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomName");
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
if (!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, chestContents);
if (compound.hasKey("CustomName", 8)) compound.setString("CustomName", this.customName);
return compound;
}
@Override
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
return new ContainerTrashBinSmall(playerInventory, this, playerIn);
}
@Override
public String getGuiID() {
return Reference.MODID + ":trash_bin_small";
}
@Override
public NonNullList<ItemStack> getItems() {
return this.chestContents;
}
@Override
public void update() {
if (!this.world.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + pos.getX() + pos.getY() + pos.getZ()) % 200 == 0)
{
this.numPlayersUsing = 0;
float f = 5.0F;
for (EntityPlayer entityPlayer : this.world.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double)((float)pos.getX() - 5.0F), (double)((float)pos.getY() - 5.0F), (double)((float)pos.getZ() - 5.0F), (double)((float)(pos.getX() + 1) + 5.0F), (double)((float)(pos.getY() + 1) + 5.0F), (double)((float)(pos.getZ() + 1) + 5.0F))))
{
if (entityPlayer.openContainer instanceof ContainerTrashBinSmall)
{
if (((ContainerTrashBinSmall)entityPlayer.openContainer).getChestInventory() == this)
{
++this.numPlayersUsing;
}
}
}
}
this.prevLidAngle = this.lidAngle;
float f1 = 0.1F;
if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F)
{
double d1 = (double)pos.getX() + 0.5D;
double d2 = (double)pos.getZ() + 0.5D;
this.world.playSound((EntityPlayer)null, d1, (double)pos.getY() + 0.5D, d2, SoundEvents.BLOCK_CLOTH_PLACE, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
{
float f2 = this.lidAngle;
if (this.numPlayersUsing > 0)
{
this.lidAngle += 0.1F;
}
else
{
this.lidAngle -= 0.1F;
}
if (this.lidAngle > 1.0F)
{
this.lidAngle = 1.0F;
}
float f3 = 0.5F;
if (this.lidAngle < 0.5F && f2 >= 0.5F)
{
double d3 = (double)pos.getX() + 0.5D;
double d0 = (double)pos.getZ() + 0.5D;
this.world.playSound((EntityPlayer)null, d3, (double)pos.getY() + 0.5D, d0, SoundEvents.BLOCK_CLOTH_BREAK, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.lidAngle < 0.0F)
{
this.lidAngle = 0.0F;
}
}
}
@Override
public void openInventory(EntityPlayer player) {
++this.numPlayersUsing;
this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
}
@Override
public void closeInventory(EntityPlayer player) {
--this.numPlayersUsing;
this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
}
public String getInventoryName()
{
return TrashBinSmall.getUnlocName();
}
}
также сам блок TrashBinSmall.java:
public class TrashBinSmall extends BlockContainer {
private static final AxisAlignedBB BOUNDING_BOX = new AxisAlignedBB(0.200D, 0.0D, 0.125D, 0.800D, 0.9465D, 0.875D);
private static final AxisAlignedBB COLLISION_BOX = new AxisAlignedBB(0.200D, 0.0D, 0.125D, 0.800D, 0.9465D, 0.875D);
private static String unlocName;
public TrashBinSmall(String name, Material material)
{
super(material);
this.setRegistryName(name);
this.setUnlocalizedName(name);
unlocName = name;
}
@SideOnly(Side.CLIENT)
public void initModel() {
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
}
@Override
@SideOnly(Side.CLIENT)
public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess worldIn, BlockPos pos, EnumFacing side) {
return false;
}
@Override
public boolean isBlockNormalCube(IBlockState blockState) {
return false;
}
@Override
public boolean isOpaqueCube(IBlockState blockState) {
return false;
}
@Override
public boolean isFullCube(IBlockState blockState) {
return false;
}
@Override
public boolean isFullBlock(IBlockState state) { return false; }
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return BOUNDING_BOX;
}
public static AxisAlignedBB getCollisionBox() {
return COLLISION_BOX;
}
public static String getUnlocName() {
return unlocName;
};
private final Random rand = new Random();
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
// Проверка на сервер
if(worldIn.isRemote) return true;
TileEntity te = worldIn.getTileEntity(pos);
if (te != null && te instanceof TileEntityTrashBinSmall)
{
playerIn.openGui(ExileZDecorative.instance, Reference.GUI_TRASH_BIN, worldIn, pos.getX(), pos.getY(), pos.getZ());
return true;
}
return false;
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
// Проверка на сервер
if (worldIn.isRemote) return;
ArrayList drops = new ArrayList();
TileEntity teRaw = worldIn.getTileEntity(pos);
if (teRaw != null && teRaw instanceof TileEntityTrashBinSmall)
{
TileEntityTrashBinSmall te = (TileEntityTrashBinSmall) teRaw;
for (int i = 0; i < te.getSizeInventory(); i++)
{
ItemStack stack = te.getStackInSlot(i);
if (stack != null) drops.add(stack.copy());
}
}
for (int i = 0; i < drops.size(); i++)
{
EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, (ItemStack) drops.get(i));
item.setVelocity((rand.nextDouble() - 0.5) * 0.25, rand.nextDouble() * 0.5 * 0.25, (rand.nextDouble() - 0.5) * 0.25);
worldIn.spawnEntity(item);
// Обновление выхода редстоуна для компараторов
// (это если кто-нибудь захочет отследить зависимость силы редстоуна от кол-ва предметов в хранилище)
worldIn.updateComparatorOutputLevel(pos, this);
}
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
if(stack.hasDisplayName()) {
TileEntity tileEntity = worldIn.getTileEntity(pos);
if(tileEntity instanceof TileEntityTrashBinSmall) {
((TileEntityTrashBinSmall)tileEntity).setCustomName(stack.getDisplayName());
((TileEntityTrashBinSmall)tileEntity).setLootTable(LootTableList.LOOT_TRASH_BIN_SMALL, worldIn.rand.nextLong());
System.out.println(LootTableList.LOOT_TRASH_BIN_SMALL);
}
}
}
public TileEntity createNewTileEntity(World world, int par2)
{
return new TileEntityTrashBinSmall();
}
@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
return EnumBlockRenderType.MODEL;
}
}
Последнее редактирование: