use of org.spongepowered.api.event.action.InteractEvent in project Nucleus by NucleusPowered.
the class PowertoolListener method onUserInteract.
@Listener
@Exclude(InteractBlockEvent.class)
public void onUserInteract(final InteractEvent event, @Root Player player) {
// No item in hand or no permission -> no powertool.
if (!permissionRegistry.testBase(player) || !player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
return;
}
// Get the item and the user.
ItemType item = player.getItemInHand(HandTypes.MAIN_HAND).get().getType();
PowertoolUserDataModule user;
try {
user = Nucleus.getNucleus().getUserDataManager().getUnchecked(player).get(PowertoolUserDataModule.class);
} catch (Exception e) {
Nucleus.getNucleus().printStackTraceIfDebugMode(e);
return;
}
// If the powertools are toggled on.
if (user.isPowertoolToggled()) {
// Execute all powertools if they exist.
user.getPowertoolForItem(item).ifPresent(x -> {
// Cancel the interaction.
event.setCancelled(true);
final Player interacting;
if (event instanceof InteractEntityEvent && ((InteractEntityEvent) event).getTargetEntity() instanceof Player) {
interacting = (Player) ((InteractEntityEvent) event).getTargetEntity();
} else {
interacting = null;
}
// Run each command.
if (interacting == null && x.stream().allMatch(i -> i.contains("{{subject}}"))) {
player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("powertool.playeronly"));
return;
}
x.forEach(s -> {
if (s.contains("{{subject}}")) {
if (interacting != null) {
s = s.replace("{{subject}}", interacting.getName());
} else {
// Don't execute when no subject is in the way.
return;
}
}
Sponge.getCommandManager().process(player, s);
});
});
}
}
Aggregations