Jump to content

[1.15.x] Problem With Spawning Entity


Scaleios

Recommended Posts

I've been digging around for a while, looking for a way to spawn items in the world. In essence, I have the player, I want to take an ItemStack from their inventory (so far so good), and drop it. Now I know that my question is about entity spawning, but this says dropping, but I couldn't get either to work. The first thing I did was try to use ClientPlayerEntity.dropItem, which did nothing, so I assumed that was because I used InvWrapper.extractItem beforehand. That is when I transitioned to spawning. I tried to create a new ItemEntity and use World.addEntity on it. I know I have a valid ItemEntity, and I saw some form of this idea used in other projects & minecraft code. When executing however, nothing happens. I also tried to add the ItemStack back to the inventory and drop it, but that did nothing either.

I am not sure where to go from here. Here is my code (Really starts at Functions.java):

 

Mod.java

@Mod("Mod")
public class Mod {
  MinecraftForge.EVENT_BUS.register(clientEventHandlers);
}

 

ClientEventHandlers.java

class ClientEventHandlers {
  @SubscribeEvent
  public void onKeyRelease(GuiScreenEvent.KeyboardKeyReleasedEvent.Post event) {
    if (true) { // Ignore what this says, this is just used for simplicity
      Functions.myFunction();
    }
  }
}

 

Functions.java

class Functions {
  static void myFunction(){
    ClientPlayerEntity player = Minecraft.getInstance().player;
    InvWrapper invWrapper = new InvWrapper(player.inventory);
    ArrayList<ItemStack> removed;
    removed.add(invWrapper.extractItems(my, logic, false); // Removing item from inventory, works as expected
    
    // Important Bit Starts here
    BlockPos pos = player.getPosition();
    for (ItemStack itemStack: removed){
      // Each of the following comment blocks is something I tried, all to the same end
      /*
      ItemEntity itemEntity = new ItemEntity(player.world, pos.getX(), pos.getY(), pos.getZ(), itemStack.getStack());
      player.world.addEntity(itemEntity); */
      
      /*
      ItemEntity itemEntity = new ItemEntity(player.world, pos.getX(), pos.getY(), pos.getZ(), itemStack.getStack());
      itemEntity.world.addEntity(itemEntity); */
      
      /*
      invWrapper.setStackInSlot(someSlot, itemStack);
      player.dropItem(itemStack, true, true); // I tried every combination of bools here */
    }
  }
}

 

(Player has to be correct because all other logic works as expected)

An example value of itemEntity: "ItemEntity['Spruce Wood'/4, l='MpServer', x=4.00, y=56.00, z=-7.00]"

Link to comment
Share on other sites

6 minutes ago, Scaleios said:

I've been digging around for a while, looking for a way to spawn items in the world. In essence, I have the player, I want to take an ItemStack from their inventory (so far so good), and drop it. Now I know that my question is about entity spawning, but this says dropping, but I couldn't get either to work. The first thing I did was try to use ClientPlayerEntity.dropItem, which did nothing, so I assumed that was because I used InvWrapper.extractItem beforehand. That is when I transitioned to spawning. I tried to create a new ItemEntity and use World.addEntity on it. I know I have a valid ItemEntity, and I saw some form of this idea used in other projects & minecraft code. When executing however, nothing happens. I also tried to add the ItemStack back to the inventory and drop it, but that did nothing either.

I am not sure where to go from here. Here is my code (Really starts at Functions.java):

 

Mod.java


@Mod("Mod")
public class Mod {
  MinecraftForge.EVENT_BUS.register(clientEventHandlers);
}

 

ClientEventHandlers.java


class ClientEventHandlers {
  @SubscribeEvent
  public void onKeyRelease(GuiScreenEvent.KeyboardKeyReleasedEvent.Post event) {
    if (true) { // Ignore what this says, this is just used for simplicity
      Functions.myFunction();
    }
  }
}

 

Functions.java


class Functions {
  static void myFunction(){
    ClientPlayerEntity player = Minecraft.getInstance().player;
    InvWrapper invWrapper = new InvWrapper(player.inventory);
    ArrayList<ItemStack> removed;
    removed.add(invWrapper.extractItems(my, logic, false); // Removing item from inventory, works as expected
    
    // Important Bit Starts here
    BlockPos pos = player.getPosition();
    for (ItemStack itemStack: removed){
      // Each of the following comment blocks is something I tried, all to the same end
      /*
      ItemEntity itemEntity = new ItemEntity(player.world, pos.getX(), pos.getY(), pos.getZ(), itemStack.getStack());
      player.world.addEntity(itemEntity); */
      
      /*
      ItemEntity itemEntity = new ItemEntity(player.world, pos.getX(), pos.getY(), pos.getZ(), itemStack.getStack());
      itemEntity.world.addEntity(itemEntity); */
      
      /*
      invWrapper.setStackInSlot(someSlot, itemStack);
      player.dropItem(itemStack, true, true); // I tried every combination of bools here */
    }
  }
}

 

(Player has to be correct because all other logic works as expected)

An example value of itemEntity: "ItemEntity['Spruce Wood'/4, l='MpServer', x=4.00, y=56.00, z=-7.00]"

Can you send us your crash.report if this still don‘t work?

New in Modding? == Still learning!

Link to comment
Share on other sites

8 minutes ago, ChampionAsh5357 said:

You cannot spawn an entity on the client. If you want to execute a spawn, you have to send a packet to the server from the client and have that packet spawn the entity.

That was it, thank you.

 

6 minutes ago, DragonITA said:

Can you send us your crash.report if this still don‘t work?

There isn't a crash, and nothing of note shows up in the logs. Thank you anyways.

 

5 minutes ago, diesieben07 said:

Note that this requires careful checks on the server to prevent malicious clients spawning infinite entities.

Noted, thank you.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.