use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.
the class ListPowertoolCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
boolean toggle = inu.isPowertoolToggled();
Map<String, List<String>> powertools = inu.getPowertools();
if (powertools.isEmpty()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.none"));
return CommandResult.success();
}
// Generate powertools.
List<Text> mesl = powertools.entrySet().stream().sorted((a, b) -> a.getKey().compareToIgnoreCase(b.getKey())).map(k -> from(inu, src, k.getKey(), k.getValue())).collect(Collectors.toList());
// Paginate the tools.
Util.getPaginationBuilder(src).title(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.header", toggle ? "&aenabled" : "&cdisabled")).padding(Text.of(TextColors.YELLOW, "-")).contents(mesl).sendTo(src);
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule 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);
});
});
}
}
use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.
the class DeletePowertoolCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
Optional<ItemStack> itemStack = src.getItemInHand(HandTypes.MAIN_HAND);
if (!itemStack.isPresent()) {
throw ReturnMessageException.fromKey("command.powertool.noitem");
}
PowertoolUserDataModule user = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
ItemType item = itemStack.get().getType();
Optional<List<String>> cmds = user.getPowertoolForItem(item);
if (cmds.isPresent() && !cmds.get().isEmpty()) {
user.clearPowertool(item);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.powertool.removed", Text.of(item)));
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.powertool.nocmds", Text.of(item)));
}
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.
the class ListPowertoolCommand method from.
private Text from(final PowertoolUserDataModule inu, Player src, String powertool, List<String> commands) {
Optional<ItemType> oit = Sponge.getRegistry().getType(ItemType.class, powertool);
MessageProvider mp = plugin.getMessageProvider();
// Create the click actions.
ClickAction viewAction = TextActions.executeCallback(pl -> Util.getPaginationBuilder(src).title(mp.getTextMessageWithFormat("command.powertool.ind.header", powertool)).padding(Text.of(TextColors.GREEN, "-")).contents(commands.stream().map(x -> Text.of(TextColors.YELLOW, x)).collect(Collectors.toList())).sendTo(src));
ClickAction deleteAction = TextActions.executeCallback(pl -> {
inu.clearPowertool(powertool);
pl.sendMessage(mp.getTextMessageWithFormat("command.powertool.removed", powertool));
});
TextColor tc = oit.map(itemType -> TextColors.YELLOW).orElse(TextColors.GRAY);
// id - [View] - [Delete]
return Text.builder().append(Text.of(tc, powertool)).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.view")).color(TextColors.YELLOW).onClick(viewAction).build()).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.delete")).color(TextColors.DARK_RED).onClick(deleteAction).build()).build();
}
use of io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule in project Nucleus by NucleusPowered.
the class PowertoolCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
ItemStack itemStack = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> ReturnMessageException.fromKey("command.powertool.noitem"));
Optional<String> command = args.getOne(commandKey);
PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
return command.map(s -> setPowertool(src, inu, itemStack.getType(), s)).orElseGet(() -> viewPowertool(src, inu, itemStack));
}
Aggregations