use of org.spongepowered.api.command.CommandSource in project Skree by Skelril.
the class WildernessMetaCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
WorldService service = Sponge.getServiceManager().provideUnchecked(WorldService.class);
PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
WildernessWorldWrapper wrapper = service.getEffectWrapper(WildernessWorldWrapper.class).get();
List<Text> result = wrapper.getMetaInformation().stream().sorted(Comparator.comparing(a -> a.getKey().getName())).map(this::createLine).collect(Collectors.toList());
pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Meta Info List")).padding(Text.of(" ")).sendTo(src);
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandSource in project Skree by Skelril.
the class WorldListCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
WorldService service = Sponge.getServiceManager().provideUnchecked(WorldService.class);
List<WorldEffectWrapper> worldEffectWrapperList = service.getEffectWrappers().stream().sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList());
for (WorldEffectWrapper wrapper : worldEffectWrapperList) {
String worldType = wrapper.getName();
if (!src.hasPermission("skree.world." + worldType.toLowerCase() + ".teleport")) {
continue;
}
src.sendMessage(Text.of(TextColors.GOLD, "Available ", worldType, " worlds (click to teleport):"));
for (World world : wrapper.getWorlds()) {
String worldName = world.getName();
String prettyName = worldName.replaceAll("_", " ");
src.sendMessage(Text.of(TextColors.YELLOW, TextActions.runCommand("/world " + worldName), TextActions.showText(Text.of("Teleport to " + prettyName)), " - ", prettyName));
}
}
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandSource in project SpongeCommon by SpongePowered.
the class CommandState method unwind.
@Override
public void unwind(CommandPhaseContext phaseContext) {
Optional<EntityPlayer> playerSource = phaseContext.getSource(EntityPlayer.class);
if (playerSource.isPresent()) {
// Post event for inventory changes
((IMixinInventoryPlayer) playerSource.get().inventory).setCapture(false);
List<SlotTransaction> list = ((IMixinInventoryPlayer) playerSource.get().inventory).getCapturedTransactions();
if (!list.isEmpty()) {
ChangeInventoryEvent event = SpongeEventFactory.createChangeInventoryEvent(Sponge.getCauseStackManager().getCurrentCause(), ((Inventory) playerSource.get().inventory), list);
SpongeImpl.postEvent(event);
PacketPhaseUtil.handleSlotRestore(playerSource.get(), null, list, event.isCancelled());
list.clear();
}
}
final CommandSource sender = phaseContext.getSource(CommandSource.class).orElseThrow(TrackingUtil.throwWithContext("Expected to be capturing a Command Sender, but none found!", phaseContext));
phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(list -> TrackingUtil.processBlockCaptures(list, this, phaseContext));
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(sender);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PLACEMENT);
phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
// TODO the entity spawn causes are not likely valid,
// need to investigate further.
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
final boolean isPlayer = sender instanceof Player;
final Player player = isPlayer ? (Player) sender : null;
for (Entity entity : spawnEntityEvent.getEntities()) {
if (isPlayer) {
EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
});
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(sender);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
phaseContext.getCapturedEntityDropSupplier().acceptIfNotEmpty(uuidItemStackMultimap -> {
for (Map.Entry<UUID, Collection<ItemDropData>> entry : uuidItemStackMultimap.asMap().entrySet()) {
final UUID key = entry.getKey();
@Nullable net.minecraft.entity.Entity foundEntity = null;
for (WorldServer worldServer : WorldManager.getWorlds()) {
final net.minecraft.entity.Entity entityFromUuid = worldServer.getEntityFromUuid(key);
if (entityFromUuid != null) {
foundEntity = entityFromUuid;
break;
}
}
final Optional<Entity> affectedEntity = Optional.ofNullable((Entity) foundEntity);
if (!affectedEntity.isPresent()) {
continue;
}
final Collection<ItemDropData> itemStacks = entry.getValue();
if (itemStacks.isEmpty()) {
return;
}
final List<ItemDropData> items = new ArrayList<>();
items.addAll(itemStacks);
itemStacks.clear();
final WorldServer minecraftWorld = EntityUtil.getMinecraftWorld(affectedEntity.get());
if (!items.isEmpty()) {
final List<Entity> itemEntities = items.stream().map(data -> data.create(minecraftWorld)).map(EntityUtil::fromNative).collect(Collectors.toList());
Sponge.getCauseStackManager().pushCause(affectedEntity.get());
final DropItemEvent.Destruct destruct = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), itemEntities);
SpongeImpl.postEvent(destruct);
Sponge.getCauseStackManager().popCause();
if (!destruct.isCancelled()) {
final boolean isPlayer = sender instanceof Player;
final Player player = isPlayer ? (Player) sender : null;
for (Entity entity : destruct.getEntities()) {
if (isPlayer) {
EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
}
}
});
}
}
use of org.spongepowered.api.command.CommandSource in project SpongeCommon by SpongePowered.
the class MixinServerCommandManager method executeCommand.
/**
* @author zml
*
* Purpose: Reroute MC command handling through Sponge
* Reasoning: All commands should go through one system -- we need none of the MC handling code
*/
@Override
public int executeCommand(ICommandSender sender, String command) {
command = command.trim();
if (command.startsWith("/")) {
command = command.substring(1);
}
CommandSource source = WrapperCommandSource.of(sender);
CommandResult result = SpongeImpl.getGame().getCommandManager().process(source, command);
updateStat(sender, CommandResultStats.Type.AFFECTED_BLOCKS, result.getAffectedBlocks());
updateStat(sender, CommandResultStats.Type.AFFECTED_ENTITIES, result.getAffectedEntities());
updateStat(sender, CommandResultStats.Type.AFFECTED_ITEMS, result.getAffectedItems());
updateStat(sender, CommandResultStats.Type.QUERY_RESULT, result.getQueryResult());
updateStat(sender, CommandResultStats.Type.SUCCESS_COUNT, result.getSuccessCount());
return result.getSuccessCount().orElse(0);
// return super.executeCommand(sender, command); // Try Vanilla instead
}
use of org.spongepowered.api.command.CommandSource in project SpongeCommon by SpongePowered.
the class SpongeCommandFactory method sendContainerMeta.
public static void sendContainerMeta(CommandSource src, CommandContext args, String argumentName) {
for (PluginContainer container : args.<PluginContainer>getAll(argumentName)) {
Text.Builder builder = Text.builder().append(title(container.getName()));
container.getVersion().ifPresent(version -> builder.append(Text.of((" v" + version))));
appendPluginMeta(builder, "ID", container.getId());
appendPluginMeta(builder, "Description", container.getDescription());
appendPluginMeta(builder, "URL", container.getUrl().map(url -> {
ClickAction.OpenUrl action = null;
try {
// make the url clickable
action = TextActions.openUrl(new URL(url));
} catch (MalformedURLException e) {
// or not
}
return Text.builder(url).onClick(action);
}));
if (!container.getAuthors().isEmpty()) {
appendPluginMeta(builder, "Authors", String.join(", ", container.getAuthors()));
}
appendPluginMeta(builder, "Main class", container.getInstance().map(instance -> instance.getClass().getCanonicalName()));
src.sendMessage(builder.build());
}
}
Aggregations