use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.
the class DataManipulatorGenerator method generateBase.
@SuppressWarnings("unchecked")
private <M extends DataManipulator<M, I>, I extends ImmutableDataManipulator<I, M>> Base<M, I> generateBase(TypeGenerator typeGenerator, PluginContainer pluginContainer, String id, String name, Class<M> manipulatorType, Class<I> immutableManipulatorType, @Nullable Class<? extends M> mutableExpansion, @Nullable Class<? extends I> immutableExpansion, @Nullable List<Method> methods, @Nullable List<Method> immutableMethods) {
final ClassWriter cwM = new ClassWriter(Opcodes.V1_8);
final ClassWriter cwI = new ClassWriter(Opcodes.V1_8);
final String mutableImplTypeName = newInternalName(manipulatorType);
final String immutableImplTypeName = newInternalName(immutableManipulatorType);
final String mutableImplClassName = mutableImplTypeName.replace('/', '.');
final String immutableImplClassName = immutableImplTypeName.replace('/', '.');
typeGenerator.generateClasses(cwM, cwI, mutableImplTypeName, immutableImplTypeName, manipulatorType, immutableManipulatorType, mutableExpansion, immutableExpansion, methods, immutableMethods);
cwM.visitEnd();
cwI.visitEnd();
byte[] bytes = cwM.toByteArray();
final Class<?> manipulatorTypeImpl = this.classLoader.defineClass(mutableImplClassName, bytes);
bytes = cwI.toByteArray();
final Class<?> immutableManipulatorTypeImpl = this.classLoader.defineClass(immutableImplClassName, bytes);
final ClassWriter cw = new ClassWriter(Opcodes.V1_8);
final String className = this.registrationGenerator.generate(cw, (Class) manipulatorType, (Class) manipulatorTypeImpl, (Class) immutableManipulatorType, (Class) immutableManipulatorTypeImpl);
bytes = cw.toByteArray();
final Class<?> registrationClass = this.classLoader.defineClass(className, bytes);
return new Base(() -> {
try {
return registrationClass.getConstructor(PluginContainer.class, String.class, String.class).newInstance(pluginContainer, id, name);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}, manipulatorTypeImpl, immutableManipulatorTypeImpl);
}
use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.
the class CommandParticleEffect method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.catalogedElement(Text.of("type"), ParticleType.class), GenericArguments.vector3d(Text.of("position")), GenericArguments.optional(GenericArguments.world(Text.of("world"))), // TODO: Tab complaining is currently throwing errors, but it's a small bug in SpongeAPI
GenericArguments.flags().valueFlag(GenericArguments.integer(Text.of("quantity")), "-quantity", "q").valueFlag(GenericArguments.vector3d(Text.of("offset")), "-offset", "o").valueFlag(GenericArguments.vector3d(Text.of("velocity")), "-velocity", "v").valueFlag(GenericArguments.vector3d(Text.of("color")), "-color", "c").valueFlag(GenericArguments.doubleNum(Text.of("scale")), "-scale", "s").valueFlag(GenericArguments.catalogedElement(Text.of("note"), NotePitch.class), "-note", "n").valueFlag(GenericArguments.catalogedElement(Text.of("block"), BlockState.class), "-block", "b").valueFlag(GenericArguments.catalogedElement(Text.of("item"), ItemType.class), "-item", "i").valueFlag(GenericArguments.catalogedElement(Text.of("potion"), PotionEffectType.class), "-potion", "p").buildWith(GenericArguments.none())).executor((src, args) -> {
final ParticleType particleType = args.<ParticleType>getOne("type").get();
final Vector3d position = args.<Vector3d>getOne("position").get();
final World world = args.<WorldProperties>getOne("world").map(props -> Sponge.getServer().getWorld(props.getUniqueId()).get()).orElseGet(((Locatable) src)::getWorld);
final ParticleEffect.Builder builder = ParticleEffect.builder().type(particleType);
args.<Integer>getOne("quantity").ifPresent(builder::quantity);
args.<Vector3d>getOne("offset").ifPresent(builder::offset);
args.<Vector3d>getOne("velocity").ifPresent(builder::velocity);
args.<Vector3d>getOne("color").ifPresent(color -> builder.option(ParticleOptions.COLOR, Color.of(color.toInt())));
args.<NotePitch>getOne("note").ifPresent(note -> builder.option(ParticleOptions.NOTE, note));
args.<Double>getOne("scale").ifPresent(scale -> builder.option(ParticleOptions.SCALE, scale));
args.<BlockState>getOne("block").ifPresent(blockState -> builder.option(ParticleOptions.BLOCK_STATE, blockState));
args.<ItemType>getOne("item").ifPresent(item -> builder.option(ParticleOptions.ITEM_STACK_SNAPSHOT, new LanternItemStack(item).createSnapshot()));
args.<PotionEffectType>getOne("potion").ifPresent(type -> builder.option(ParticleOptions.POTION_EFFECT_TYPE, type));
world.spawnParticles(builder.build(), position);
src.sendMessage(t("Successfully spawned the particle %s", particleType.getName()));
return CommandResult.success();
});
}
use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.
the class CommandScoreboard method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none())).child(CommandSpec.builder().child(CommandSpec.builder().executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
final Set<Objective> objectives = scoreboard.getObjectives();
if (objectives.isEmpty()) {
throw new CommandException(t("commands.scoreboard.objectives.list.empty"));
}
src.sendMessage(tb("commands.scoreboard.objectives.list.count", objectives.size()).color(TextColors.DARK_GREEN).build());
objectives.forEach(objective -> src.sendMessage(t("commands.scoreboard.objectives.list.entry", objective.getName(), objective.getDisplayName(), objective.getCriterion().getName())));
return CommandResult.success();
}).build(), "list").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("name")), GenericArguments.catalogedElement(Text.of("criterion"), Criterion.class), GenericArguments.flags().valueFlag(GenericArguments.catalogedElement(Text.of("display-mode"), ObjectiveDisplayMode.class), "-display-mode", "-dm", "d").buildWith(GenericArguments.none()), GenericArguments2.remainingString(Text.of("display-name"))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
final String name = args.<String>getOne("name").get();
final Criterion criterion = args.<Criterion>getOne("criterion").get();
if (scoreboard.getObjective(name).isPresent()) {
throw new CommandException(t("commands.scoreboard.objectives.add.alreadyExists", name));
}
if (name.length() > 16) {
throw new CommandException(t("commands.scoreboard.objectives.add.tooLong", name, 16));
}
String displayName = args.<String>getOne("display-name").orElse(null);
if (displayName != null && displayName.length() > 32) {
throw new CommandException(t("commands.scoreboard.objectives.add.displayTooLong", displayName, 32));
}
Objective.Builder builder = Objective.builder().name(name).criterion(criterion);
if (displayName != null && !displayName.isEmpty()) {
builder.displayName(Text.of(displayName));
}
args.<ObjectiveDisplayMode>getOne("display-mode").ifPresent(builder::objectiveDisplayMode);
scoreboard.addObjective(builder.build());
src.sendMessage(t("commands.scoreboard.objectives.add.success", name));
return CommandResult.success();
}).build(), "add").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("name"))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
final String name = args.<String>getOne("name").get();
scoreboard.removeObjective(scoreboard.getObjective(name).orElseThrow(() -> new CommandException(t("commands.scoreboard.objectiveNotFound", name))));
src.sendMessage(t("commands.scoreboard.objectives.remove.success", name));
return CommandResult.success();
}).build(), "remove").child(CommandSpec.builder().arguments(GenericArguments.catalogedElement(Text.of("display-slot"), DisplaySlot.class), GenericArguments.optional(GenericArguments.string(Text.of("name")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
final Optional<String> optName = args.<String>getOne("name");
DisplaySlot displaySlot = args.<DisplaySlot>getOne("display-slot").get();
Objective objective = null;
if (optName.isPresent()) {
final String name = optName.get();
objective = scoreboard.getObjective(name).orElseThrow(() -> new CommandException(t("commands.scoreboard.objectiveNotFound", name)));
src.sendMessage(t("commands.scoreboard.objectives.setdisplay.successSet", displaySlot.getName(), name));
} else {
src.sendMessage(t("commands.scoreboard.objectives.setdisplay.successCleared", displaySlot.getName()));
}
scoreboard.updateDisplaySlot(objective, displaySlot);
return CommandResult.success();
}).build(), "setdisplay").build(), "objectives").child(CommandSpec.builder().child(CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.string(Text.of("target")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
int result;
if (args.hasAny("target")) {
String entity = args.<String>getOne("target").get();
Set<Score> scores = scoreboard.getScores(Text.of(entity));
if (scores.isEmpty()) {
throw new CommandException(t("commands.scoreboard.players.list.player.empty"));
}
result = scores.size();
src.sendMessage(tb("commands.scoreboard.players.list.player.count", result, entity).color(TextColors.DARK_GREEN).build());
scores.forEach(score -> score.getObjectives().forEach(objective -> src.sendMessage(t("commands.scoreboard.players.list.player.entry", score.getScore(), objective.getDisplayName(), objective.getName()))));
} else {
Set<Text> names = scoreboard.getScores().stream().map(Score::getName).collect(Collectors.toSet());
if (names.isEmpty()) {
throw new CommandException(t("commands.scoreboard.players.list.empty"));
}
result = names.size();
src.sendMessage(tb("commands.scoreboard.players.list.count", result).color(TextColors.DARK_GREEN).build());
src.sendMessage(Text.joinWith(Text.of(", "), names));
}
return CommandResult.builder().successCount(1).queryResult(result).build();
}).build(), "list").child(createPlayerScoreSpec(Score::setScore), "set").child(createPlayerScoreSpec((score, value) -> score.setScore(score.getScore() + value)), "add").child(createPlayerScoreSpec((score, value) -> score.setScore(score.getScore() - value)), "remove").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("target")), GenericArguments.optional(GenericArguments.string(Text.of("objective")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
String target = args.<String>getOne("target").get();
if (args.hasAny("objective")) {
String objectiveName = args.<String>getOne("objective").get();
Objective objective = scoreboard.getObjective(objectiveName).orElseThrow(() -> new CommandException(t("commands.scoreboard.objectiveNotFound", objectiveName)));
objective.removeScore(Text.of(target));
src.sendMessage(t("commands.scoreboard.players.resetscore.success", objectiveName, target));
} else {
scoreboard.removeScores(Text.of(target));
src.sendMessage(t("commands.scoreboard.players.reset.success", target));
}
return CommandResult.success();
}).build(), "reset").build(), "players").child(CommandSpec.builder().child(CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.string(Text.of("name")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
int result;
if (args.hasAny("name")) {
String teamName = args.<String>getOne("name").get();
Team team = scoreboard.getTeam(teamName).orElseThrow(() -> new CommandException(t("commands.scoreboard.teamNotFound", teamName)));
Set<Text> members = team.getMembers();
if (members.isEmpty()) {
throw new CommandException(t("commands.scoreboard.teams.list.player.empty", teamName));
}
result = members.size();
src.sendMessage(tb("commands.scoreboard.teams.list.player.count", result, teamName).color(TextColors.DARK_GREEN).build());
src.sendMessage(Text.joinWith(Text.of(", "), members));
} else {
Set<Team> teams = scoreboard.getTeams();
if (teams.isEmpty()) {
throw new CommandException(t("commands.scoreboard.teams.list.empty"));
}
result = teams.size();
src.sendMessage(tb("commands.scoreboard.teams.list.count", result).color(TextColors.DARK_GREEN).build());
teams.forEach(team -> src.sendMessage(t("commands.scoreboard.teams.list.entry", team.getName(), team.getDisplayName(), team.getMembers().size())));
}
return CommandResult.builder().successCount(1).queryResult(result).build();
}).build(), "list").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("name")), GenericArguments.optional(GenericArguments2.remainingString(Text.of("display-name")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
final String teamName = args.<String>getOne("name").get();
if (scoreboard.getTeam(teamName).isPresent()) {
throw new CommandException(t("commands.scoreboard.teams.add.alreadyExists", teamName));
}
if (teamName.length() > 16) {
throw new CommandException(t("commands.scoreboard.teams.add.tooLong", teamName, 16));
}
String displayName = args.<String>getOne("display-name").orElse(null);
if (displayName != null && displayName.length() > 32) {
throw new CommandException(t("commands.scoreboard.teams.add.displayTooLong", displayName, 32));
}
Team.Builder teamBuilder = Team.builder().name(teamName);
if (displayName != null && !displayName.isEmpty()) {
teamBuilder.displayName(Text.of(displayName));
}
scoreboard.registerTeam(teamBuilder.build());
src.sendMessage(t("commands.scoreboard.teams.add.success", teamName));
return CommandResult.success();
}).build(), "add").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("name"))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
String teamName = args.<String>getOne("name").get();
final Team team = scoreboard.getTeam(teamName).orElseThrow(() -> new CommandException(t("commands.scoreboard.teamNotFound", teamName)));
team.unregister();
src.sendMessage(t("commands.scoreboard.teams.remove.success", teamName));
return CommandResult.success();
}).build(), "remove").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("name"))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
String teamName = args.<String>getOne("name").get();
final Team team = scoreboard.getTeam(teamName).orElseThrow(() -> new CommandException(t("commands.scoreboard.teamNotFound", teamName)));
Set<Text> members = team.getMembers();
if (members.isEmpty()) {
throw new CommandException(t("commands.scoreboard.teams.empty.alreadyEmpty", teamName));
}
int result = members.size();
((LanternTeam) team).removeMembers(members);
src.sendMessage(t("commands.scoreboard.teams.empty.success", result, teamName));
return CommandResult.builder().successCount(1).queryResult(result).build();
}).build(), "empty").child(CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.string(Text.of("name"))), GenericArguments.optional(GenericArguments2.remainingStringArray(Text.of("players")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
String teamName = args.<String>getOne("name").orElse(null);
final Team team = teamName == null ? null : scoreboard.getTeam(teamName).orElse(null);
Set<Text> members = args.<String[]>getOne("players").map(array -> Arrays.stream(array).map(LanternTexts::fromLegacy).collect(Collectors.toSet())).orElse(new HashSet<>());
// that wants to leave a team
if (teamName != null && team == null) {
members.add(LanternTexts.fromLegacy(teamName));
}
// If there are no members found, use the source if possible
if (members.isEmpty() && src instanceof Player) {
members.add(((Player) src).getTeamRepresentation());
}
// If there is a team specified, remove the members from a specific team
Collection<Team> teams = team == null ? scoreboard.getTeams() : Collections.singleton(team);
List<Text> failedMembers = null;
for (Team team0 : teams) {
List<Text> failedMembers0 = ((LanternTeam) team0).removeMembers(members);
if (failedMembers == null) {
failedMembers = failedMembers0;
} else {
failedMembers.retainAll(failedMembers0);
}
}
if (failedMembers != null) {
members.removeAll(failedMembers);
}
int result = members.size();
if (result > 0) {
src.sendMessage(t("commands.scoreboard.teams.leave.success", result, Text.joinWith(Text.of(", "), members)));
}
if (failedMembers != null && failedMembers.size() > 0) {
src.sendMessage(error(t("commands.scoreboard.teams.leave.failure", failedMembers.size(), Text.joinWith(Text.of(", "), failedMembers))));
}
return CommandResult.builder().successCount(1).queryResult(result).build();
}).build(), "leave").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("name")), GenericArguments.optional(GenericArguments2.remainingStringArray(Text.of("players")))).executor((src, args) -> {
// Get the scoreboard of the world the command source is located in
final Scoreboard scoreboard = CommandHelper.getWorld(src, args).getScoreboard();
String teamName = args.<String>getOne("name").get();
final Team team = scoreboard.getTeam(teamName).orElseThrow(() -> new CommandException(t("commands.scoreboard.teamNotFound", teamName)));
Set<Text> members = args.<String[]>getOne("players").map(array -> Arrays.stream(array).map(LanternTexts::fromLegacy).collect(Collectors.toSet())).orElse(new HashSet<>());
// If there are no members found, use the source if possible
if (members.isEmpty() && src instanceof Player) {
members.add(((Player) src).getTeamRepresentation());
}
List<Text> failedMembers = ((LanternTeam) team).addMembers(members);
int result = members.size();
if (result > 0) {
src.sendMessage(t("commands.scoreboard.teams.join.success", result, teamName, Text.joinWith(Text.of(", "), members)));
}
if (failedMembers.size() > 0) {
src.sendMessage(error(t("commands.scoreboard.teams.join.failure", failedMembers.size(), teamName, Text.joinWith(Text.of(", "), failedMembers))));
}
return CommandResult.builder().successCount(1).queryResult(result).build();
}).build(), "join").build(), "teams");
}
use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.
the class CommandOp method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(new CommandElement(Text.of("player")) {
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
return args.next();
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
final String prefix = args.nextIfPresent().orElse("");
final UserConfig<OpsEntry> config = Lantern.getGame().getOpsConfig();
return Lantern.getGame().getGameProfileManager().getCache().getProfiles().stream().filter(p -> p.getName().isPresent() && !config.getEntryByUUID(p.getUniqueId()).isPresent()).map(p -> p.getName().get()).filter(new StartsWithPredicate(prefix)).collect(ImmutableList.toImmutableList());
}
}, GenericArguments.optional(GenericArguments.integer(Text.of("level")))).executor((src, args) -> {
String playerName = args.<String>getOne("player").get();
UserConfig<OpsEntry> config = Lantern.getGame().getOpsConfig();
if (!(src instanceof ConsoleSource) && args.hasAny("level")) {
throw new CommandException(Text.of("Only the console may specify the op level."));
}
int opLevel = args.<Integer>getOne("level").orElse(Lantern.getGame().getGlobalConfig().getDefaultOpPermissionLevel());
Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
if (error != null) {
src.sendMessage(t("commands.op.failed", playerName));
} else {
src.sendMessage(t("commands.op.success", playerName));
config.addEntry(new OpsEntry(((LanternGameProfile) profile).withoutProperties(), opLevel));
}
});
return CommandResult.success();
});
}
use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.
the class CommandTp method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.optional(GenericArguments.player(Text.of("target"))), GenericArguments.firstParsing(GenericArguments.player(Text.of("destination")), GenericArguments.seq(/*
GenericArguments.flags()
.valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY),
"-world", "w")
.buildWith(GenericArguments.none()),
*/
GenericArguments.optional(GenericArguments.world(CommandHelper.WORLD_KEY)), GenericArguments2.targetedRelativeVector3d(Text.of("coordinates")), GenericArguments.optional(GenericArguments.seq(GenericArguments2.relativeDoubleNum(Text.of("y-rot")), GenericArguments2.relativeDoubleNum(Text.of("x-rot"))))))).executor((src, args) -> {
Player target = args.<Player>getOne("target").orElse(null);
if (target == null) {
if (!(src instanceof Player)) {
throw new CommandException(t("The target parameter is only optional for players."));
}
target = (Player) src;
}
final Optional<Player> optDestination = args.getOne("destination");
if (optDestination.isPresent()) {
final Player destination = optDestination.get();
target.setTransform(destination.getTransform());
src.sendMessage(t("commands.tp.success", target.getName(), destination.getName()));
} else {
final RelativeVector3d coords = args.<RelativeVector3d>getOne("coordinates").get();
final Transform<World> transform = target.getTransform();
World world = args.<WorldProperties>getOne(CommandHelper.WORLD_KEY).flatMap(p -> Lantern.getServer().getWorld(p.getUniqueId())).orElse(transform.getExtent());
Vector3d position = coords.applyToValue(transform.getPosition());
final Optional<RelativeDouble> optYRot = args.getOne("y-rot");
if (optYRot.isPresent()) {
final Vector3d rot = transform.getRotation();
double xRot = args.<RelativeDouble>getOne("x-rot").get().applyToValue(rot.getX());
double yRot = args.<RelativeDouble>getOne("y-rot").get().applyToValue(rot.getY());
double zRot = rot.getZ();
target.setLocationAndRotation(new Location<>(world, position), new Vector3d(xRot, yRot, zRot));
} else {
target.setLocation(new Location<>(world, position));
}
src.sendMessage(t("commands.tp.success.position", target.getName(), formatDouble(position.getX()), formatDouble(position.getY()), formatDouble(position.getZ()), world.getName()));
}
return CommandResult.success();
});
}
Aggregations