Locking chests?

Hey guys, if anyone could offer any suggestions that would be great. I am trying to make a plugin similar to Lockette. Right now I am having the plugin run “blockdata” and putting a lock on the chest that way. Is there anything available in Sponge that allows me to do this better? Like a chest open event or a method in the Chest class? I am open all suggestions.

Look at InteractEvent.Secondary.

1 Like

https://jd.spongepowered.org/org/spongepowered/api/event/entity/InteractEntityEvent.html
(there’s .primary and .secondary too)

Do TileEntities count as Entities in this context?

@gravityfox No, since TileEntities are basically extensions to blocks you’ll want to use InteractBlockEvent

Okay, I was just making sure. @Tzk Linked InteractEntityEvent and I was like wat…

I wasn’t sure myself and just went with InteractEntityEvent. The other one would be:
https://jd.spongepowered.org/org/spongepowered/api/event/block/InteractBlockEvent.html

Okay, I see how that event would be executed upon a player right clicking a chest. But how do I prevent the chest from interacting with the player once that event is called? (Thank you for the responses btw)

All you do is cancel the event.

The chest won’t open if the right click event is canceled.

It’s like the player never actually clicked the chest.

2 Likes

Ah I see, got it working. One last question. Within the event, it gives me a NoSuchMethod error in the console if I try to get the player and use player.sendMessages(Texts.of(some string here)); For example,
if(event.getCause().first(Player.class).isPresent()) {
Player player = event.getCause().first(Player.class).get();
player.sendMessages(Texts.of(player.getName())); //Just a test
}

You’re probably testing on an old spongeforge build. Update to the latest

1 Like

Remember when I said one last question? That was a joke. What’s the best way to get the block in front of a chest?

Get the direction the chest is facing:

Direction d = (Direction) block.getTraitValue(EnumTraits.CHEST_FACING).get();

Then get the vector of that direction and add it to the chest location

Vector3i pos = d.toVector3d().toInt().add(chestPos);

then get the block at this position

BlockState block = world.getBlock(pos);
2 Likes

Question: why are you so awesome? Thank you.

1 Like

Vector3i (which I assume also contains the method toVector3d, or some similar class does) does not appear to have an import. Is this a new addition to the api? or is it something I need to get separately?

com.flowpowered.math… is the package they are in

I just added the flow-math jar to my dependencies and its working now.

@simon816 I am getting an abstract method error when calling block.getTraitValue

Well it’s definitely implemented so you’re probably using an old version of spongeforge
https://github.com/SpongePowered/SpongeCommon/blob/master/src/main/java/org/spongepowered/common/mixin/core/block/MixinBlockState.java#L374

Oh and make sure you’ve got a working gradle setup to make sure dependencies are managed properly.