Jump to content

Recommended Posts

Posted

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]"

Posted
  On 3/22/2020 at 2:06 PM, 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]"

Expand  

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

New in Modding? == Still learning!

Posted
  On 3/22/2020 at 2:11 PM, 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.

Expand  

That was it, thank you.

 

  On 3/22/2020 at 2:13 PM, DragonITA said:

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

Expand  

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

 

  On 3/22/2020 at 2:14 PM, diesieben07 said:

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

Expand  

Noted, thank you.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i was playing a custom modpack and i cant open the world anymore.  here are the logs :  [15:28:31] [Server thread/ERROR]:Encountered an unexpected exception net.minecraftforge.fml.config.ConfigFileTypeHandler$ConfigLoadingException: Failed loading config file jei-server.toml of type SERVER for modid jei at net.minecraftforge.fml.config.ConfigFileTypeHandler.lambda$reader$1(ConfigFileTypeHandler.java:47) ~[fmlcore-1.20.1-47.4.0.jar%23305!/:?] at net.minecraftforge.fml.config.ConfigTracker.openConfig(ConfigTracker.java:60) ~[fmlcore-1.20.1-47.4.0.jar%23305!/:?] at net.minecraftforge.fml.config.ConfigTracker.lambda$loadConfigs$1(ConfigTracker.java:50) ~[fmlcore-1.20.1-47.4.0.jar%23305!/:?] at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?] at java.util.Collections$SynchronizedCollection.forEach(Collections.java:2131) ~[?:?] at net.minecraftforge.fml.config.ConfigTracker.loadConfigs(ConfigTracker.java:50) ~[fmlcore-1.20.1-47.4.0.jar%23305!/:?] at net.minecraftforge.server.ServerLifecycleHooks.handleServerAboutToStart(ServerLifecycleHooks.java:96) ~[forge-1.20.1-47.4.0-universal.jar%23309!/:?] at net.minecraft.client.server.IntegratedServer.m_7038_(IntegratedServer.java:62) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:634) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at java.lang.Thread.run(Thread.java:840) ~[?:?] Caused by: com.electronwill.nightconfig.core.io.ParsingException: Not enough data available at com.electronwill.nightconfig.core.io.ParsingException.notEnoughData(ParsingException.java:22) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.io.ReaderInput.directReadChar(ReaderInput.java:36) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.io.AbstractInput.readChar(AbstractInput.java:49) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.io.AbstractInput.readCharsUntil(AbstractInput.java:123) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.toml.TableParser.parseKey(TableParser.java:166) ~[toml-3.6.4.jar%2394!/:?] at com.electronwill.nightconfig.toml.TableParser.parseDottedKey(TableParser.java:145) ~[toml-3.6.4.jar%2394!/:?] at com.electronwill.nightconfig.toml.TableParser.parseNormal(TableParser.java:55) ~[toml-3.6.4.jar%2394!/:?] at com.electronwill.nightconfig.toml.TomlParser.parse(TomlParser.java:44) ~[toml-3.6.4.jar%2394!/:?] at com.electronwill.nightconfig.toml.TomlParser.parse(TomlParser.java:37) ~[toml-3.6.4.jar%2394!/:?] at com.electronwill.nightconfig.core.io.ConfigParser.parse(ConfigParser.java:113) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.io.ConfigParser.parse(ConfigParser.java:219) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.io.ConfigParser.parse(ConfigParser.java:202) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.file.WriteSyncFileConfig.load(WriteSyncFileConfig.java:73) ~[core-3.6.4.jar%2393!/:?] at com.electronwill.nightconfig.core.file.AutosaveCommentedFileConfig.load(AutosaveCommentedFileConfig.java:85) ~[core-3.6.4.jar%2393!/:?] at net.minecraftforge.fml.config.ConfigFileTypeHandler.lambda$reader$1(ConfigFileTypeHandler.java:43) ~[fmlcore-1.20.1-47.4.0.jar%23305!/:?] ... 10 more [15:28:31] [Server thread/FATAL]:Preparing crash report with UUID 2a3982ea-90f2-492d-85e7-f1cb63906dcb [15:28:31] [Server thread/ERROR]:This crash report has been saved to: C:\Users\gueri\curseforge\minecraft\Instances\modpack je sais pas\crash-reports\crash-2025-05-29_15.28.31-server.txt [15:28:31] [Server thread/INFO]:Stopping server [15:28:31] [Server thread/INFO]:Saving players [15:28:31] [Server thread/INFO]:Saving worlds [15:28:31] [Server thread/ERROR]:Exception stopping the server java.lang.NullPointerException: Cannot invoke "net.minecraft.server.level.ServerLevel.m_6857_()" because "serverlevel2" is null at net.minecraft.server.MinecraftServer.m_129885_(MinecraftServer.java:513) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at net.minecraft.server.MinecraftServer.m_7041_(MinecraftServer.java:584) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at net.minecraft.client.server.IntegratedServer.m_7041_(IntegratedServer.java:187) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:689) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[client-1.20.1-20230612.114412-srg.jar%23304!/:?] at java.lang.Thread.run(Thread.java:840) ~[?:?]
    • Add the crash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here  
    • I recently updated my mods for prominence 2 and instead of opening like usual it just crashes. I already disable any incompatible mods and I don't know what to do. If anyone could help that would be nice. Thanks Latest log:https://mclo.gs/jsuIlZX
    • I dont know how I can elaborate more, just every time I try to create a new instance where its forge, its 1.20.1 and the forge version is 47.4.1 it doesnt work. Im really confused because it works for my friend he even showed it to me. This is on modrinth btw
  • Topics

×
×
  • Create New...

Important Information

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