Spawn / Drop item

So the DataProcessor is added - Thanks for those quick reactions.

But:

ItemStack stack = game.getRegistry().createItemBuilder()
		.itemType(ItemTypes.LAPIS_ORE)
		.quantity(1)
		.build();


  item.offer(Keys.REPRESENTED_ITEM, stack.createSnapshot());
  extent.spawnEntity(item, null);

is still spawning a stoneblock. Is something wrong on my offer?

-KAISER

I just tested it and get a stone block.
When I do System.out.println(item.offer(Keys.REPRESENTED_ITEM, stack.createSnapshot()).getType());
it shows FAILURE, so it’s definitely not completely implemented. It could be just a small thing that’s missing. I’ll take a look

Edit:
This file is just a stub, that would explain it
https://github.com/SpongePowered/SpongeCommon/blob/master/src/main/java/org/spongepowered/common/data/processor/value/RepresentedItemValueProcessor.java

@simon816 Any news?

Yeah, see this commit, gabizou has completed the rest of the implementation

Hey,

still dropping just a stone :confused:

-KAISER

@gabizou

I saw you answering questions on that on GitHub

@simon816

It’s still dropping stones with the latest versions and updates.
Any ideas?

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