evjeny.23
Members-
Posts
8 -
Joined
-
Last visited
evjeny.23's Achievements
Tree Puncher (2/8)
0
Reputation
-
@SubscribeEvent public void onLivingEntityDrops(LivingDropsEvent event) { LOGGER.info("DROP"); if (!(event.getEntityLiving() instanceof PlayerEntity)) return; BlockPos bedPos; try { bedPos = ((PlayerEntity)event.getEntityLiving()).getBedPosition().get(); } catch (NoSuchElementException e) { bedPos = new BlockPos(0, event.getEntityLiving().getPosition().getY(), 0); } World worldIn = event.getEntityLiving().getEntityWorld(); final IItemHandler[] nowHandler = new IItemHandler[1]; worldIn.setBlockState(bedPos, Blocks.BARREL.getDefaultState()); TileEntity barrelContainer = worldIn.getTileEntity(bedPos); IItemHandler itemhandler; nowHandler[0] = null; barrelContainer.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(new NonNullConsumer <IItemHandler>() { @Override public void accept(IItemHandler ih) { nowHandler[0] = ih; } }); while (nowHandler[0] == null) {}; itemhandler = nowHandler[0]; int slot = 0; for (ItemEntity item : event.getDrops()) { LOGGER.info("drop"); ItemStack leftItemsStack = itemhandler.insertItem(slot, item.getItem().copy(), false); if (!leftItemsStack.equals(ItemStack.EMPTY)) { bedPos = bedPos.north(); worldIn.setBlockState(bedPos, Blocks.BARREL.getDefaultState()); barrelContainer = worldIn.getTileEntity(bedPos); itemhandler = nowHandler[0]; slot = 0; } slot++; } } Now it raises NullPointerException.
-
I have written the following code based on a related question: final IItemHandler[] nowHandler = new IItemHandler[1]; world.setBlockState(pos, Blocks.CHEST.getDefaultState()); TileEntity chestTileEntity = worldIn.getTileEntity(pos); IItemHandler itemHandler; nowHandler[0] = null; chestTileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(new NonNullConsumer <IItemHandler>() { @Override public void accept(IItemHandler ih) { nowHandler[0] = ih; } }); while (nowHandler[0] == null) {}; itemhandler = nowHandler[0]; itemHandler.insertItem(1, new ItemStack(Items.SOME_ITEM, 1), false); // also I don't understand what "bool simulate" does But it doesn't work. Chest spawns, but there are no items in it. (There are no errors in LOG).
-
How exactly do I need to use it?
-
I need to spawn a chest with some items. I have these variables: PlayerEntity player; World world; BlockPos pos; ItemEntity[] items; // they are assigned, but I don't show the assignments here. I need to create a chest (or a barrel, or a shulker box) in world "world" at position "pos" with items "items". How can I do that? P.S. my minecraft forge version is 1.15.2.
-
Thanks! Didn't know about that. In survival mode it is fully working.
-
package com.eugeny.mobscountmod.init; import java.util.List; import com.eugeny.mobscountmod.MobsCountMod; import com.mojang.datafixers.FunctionType.Instance; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.monster.MonsterEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.potion.EffectInstance; import net.minecraft.potion.Effects; import net.minecraft.util.ActionResult; import net.minecraft.util.DamageSource; import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.registries.ObjectHolder; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; @Mod.EventBusSubscriber(modid = MobsCountMod.MOD_ID, bus = Bus.MOD) @ObjectHolder(MobsCountMod.MOD_ID) public class ItemInit { @ObjectHolder(MobsCountMod.MOD_ID + "some_item") private static final Item some_item = null; @SubscribeEvent public static void registerItems(final RegistryEvent.Register<Item> event) { event.getRegistry().register(new Item(new Item.Properties().group(ItemGroup.COMBAT)) { @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { MobsCountMod.LOGGER.info("RIGHT CLICK"); if (!playerIn.addItemStackToInventory(new ItemStack(Blocks.BEDROCK, 1))) { ItemEntity item = new ItemEntity(worldIn, playerPos.getX(), playerPos.getY(), playerPos.getZ(), new ItemStack(Items.BEDROCK, 1)); worldIn.addEntity(item); } return super.onItemRightClick(worldIn, playerIn, handIn); }; }.setRegistryName("some_item")); } } P.S. It logs "RIGHT CLICK".
-
It doesn't work somewhy. No errors and no item.
-
In older versions of Minecraft, the World.spawnEntityInWorld method could be used in order to spawn dropped ItemEntity like that (If I'm not misunderstanding): ItemEntity item = new ItemEntity(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), new ItemStack(Items.SOME_ITEM, QUANTITY)); world.spawnEntityInWorld(item); But in newer version, there is no such method. So, what's the alternative to that which works in newer versions of Minecraft?