Spawn / Drop item

The following code works for me. The player object is the player that ran the test command that executes this code.

ItemStack stack = this.game.getRegistry().createItemBuilder().itemType(ItemTypes.LAPIS_ORE).build();
Optional<Entity> optItem = player.getLocation().getExtent().createEntity(EntityTypes.ITEM, player.getLocation().getPosition());
if (optItem.isPresent()) {
    Item item = (Item) optItem.get();
    item.offer(Keys.REPRESENTED_ITEM, stack.createSnapshot());
    player.getWorld().spawnEntity(item, Cause.of(player));
}

Thanks, buddy. I got it.

@simon816

Okay, this is the extremest form of unfairness Iā€™ve encountered yet with steady changes :slight_smile: A mere 2 days after the post of ā€˜successfulā€™ codeā€¦ Iā€™m getting stone blocks spawning whether used in an event or as a routine, using LITERALLY the exact code block posted, just changing the player objectā€¦

public void SpawnItems(Player p) {
    ItemStack stack = this.game.getRegistry().createItemBuilder().itemType(ItemTypes.LAPIS_ORE).build();
    Optional<Entity> optItem = p.getLocation().getExtent().createEntity(EntityTypes.ITEM, p.getLocation().getPosition());
    if (optItem.isPresent()) {
        Item item = (Item) optItem.get();
        item.offer(Keys.REPRESENTED_ITEM, stack.createSnapshot());
        p.getWorld().spawnEntity(item, Cause.of(p));
    }
}

@Listener
public void tapBlock(InteractBlockEvent.Primary event) {
    game.getServer().getBroadcastSink().sendMessage(Texts.of("Lets tap-test a block/location"));
    if (event.getCause().last(Player.class).isPresent()) {
        Player p = event.getCause().last(Player.class).get();
        if (p.getItemInHand().isPresent()) {
            if (p.getItemInHand().get().getItem() == ItemTypes.GOLDEN_PICKAXE) {
                // do whatever test code here
                SpawnItems(event.getCause().last(Player.class).get());
                ItemStack stack = this.game.getRegistry().createItemBuilder().itemType(ItemTypes.GOLD_NUGGET).build();
                Optional<Entity> optItem = p.getLocation().getExtent().createEntity(EntityTypes.ITEM, p.getLocation().getPosition());
                if (optItem.isPresent()) {
                    Item item = (Item) optItem.get();
                    item.offer(Keys.REPRESENTED_ITEM, stack.createSnapshot());
                    p.getWorld().spawnEntity(item, Cause.of(p));
                }
            }
        }
    }
}

I tap a block with a golden-pickaxe, and all I get is two stone blocks spawned and picked up.

Eclipse maven project,

      <repositories>
    <repository>
        <id>sponge-maven-repo</id>
        <name>Sponge maven repo</name>
        <url>http://repo.spongepowered.org/maven</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
  <dependencies>
      <dependency>
          <groupId>org.spongepowered</groupId>
          <artifactId>spongeapi</artifactId>
          <version>2.1-SNAPSHOT</version>
          <exclusions>
              <exclusion>
                  <artifactId>jsr305</artifactId>
                  <groupId>com.google.code.findbugs</groupId>
              </exclusion>
          </exclusions>
      </dependency>
  </dependencies>

Forced-update of dependancies several times in the past few days and this morning. (How do I tell which specific version/build is being used?) Game is an injected object in main class definition (same as listener class)

@Inject
Game game;

ā€“
What stupid thing am I overlooking/doing wrong? Or has there been changes recently that undid this code? Note I am testing from a player from an event, not a player from a command execution, but i cant see that being the cause of differences

@TheBoomer

Without reading your post I guess itā€™s Maven.
I had the same problem and did not get the actual version.

Just include the sponge.jar as external lib and it worked for me. Maven just sucks.

Cheers

Yeahp, that worked - purged the references from the pom.xml and used the shaded api file and its dropping both correctly.

This is good to see working, but definitely bad to not make sense. But as long as the api files are generated, Iā€™ll stick with that approch - simpler project file to setup as well.

1 Like

Iā€™m guessing that our Maven-based problem is related to Latest Sponge Download links

@simon816

How to add an Enchantment or even the pure values (ATTACK_DAMAGE) to an item?

item.offer(Keys.ATTACK_DAMAGE, 10.00) 

is throwing an exception and

item.offer(Keys.ITEM_ENCHANTMENTS,

ā€¦ is just weird

Cheers

It could be that ATTACK_DAMAGE is not implemented.

I think using ITEM_ENCHANTMENTS is the correct way

1 Like

Hey,

so how to set f.e. the sharpness on level 3?
I tried a few things with the ItemEnchantment type and Enchantments.SHARPNESS but I failed :frowning:

ItemEnchantment test = new ItemEnchantment(Enchantments.SHARPNESS , 3);
item.offer(Keys.ITEM_ENCHANTMENTS, test);

@simon816

Is there any comfortable way to find out those Key Types?
I mean - Itā€™s not my first time being stucked on such little thing ā€¦

Cheers

@simon816
@blood

ItemEnchantment ItemEnchantmentSharpness = new ItemEnchantment(Enchantments.SHARPNESS, 3);
List<ItemEnchantment> ItemEnchantmentList = new ArrayList<ItemEnchantment>();
ItemEnchantmentList.add(ItemEnchantmentSharpness); 

item.offer(Keys.ITEM_ENCHANTMENTS, ItemEnchantmentList);

Should be working - But still no Enchantments on those dropped items :confused:

Can you re paste your entire listener?

I didnā€™t offer it to the ItemStack - So it works now

1 Like