Skip to content

Commit

Permalink
feat: allow entity to execute some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Oct 18, 2024
1 parent 5799347 commit e59aed8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public DifficultyCommand() {

@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot().difficulty("difficulty").exec((context, player) -> {
tree.getRoot().difficulty("difficulty").exec((context, entity) -> {
Difficulty difficulty = context.getResult(0);
player.getWorld().getWorldData().setDifficulty(difficulty);
entity.getWorld().getWorldData().setDifficulty(difficulty);
context.addOutput(TrKeys.M_COMMANDS_DIFFICULTY_SUCCESS, difficulty);
return context.success();
}, SenderType.PLAYER);
}, SenderType.ENTITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void prepareCommandTree(CommandTree tree) {
var memory = getCurrentMemoryUsage();
System.gc();
var freedMemory = memory - getCurrentMemoryUsage();
context.getSender().sendText("Memory freed: " + TextFormat.GREEN + freedMemory + " MB");
context.getSender().sendTr(TrKeys.A_COMMAND_GC_COMPLETED, freedMemory);
return context.success();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public MeCommand() {
@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.msg("message")
.msg("message", "")
.optional()
.exec(context -> {
Server.getInstance().broadcastTr(
TrKeys.M_CHAT_TYPE_EMOTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void prepareCommandTree(CommandTree tree) {
.blockType("blockType")
.blockPropertyValues("blockPropertyValues")
.optional()
.exec((context, player) -> {
.exec((context, entity) -> {
Vector3f pos = context.getResult(0);
BlockType<?> blockType = context.getResult(1);
List<BlockPropertyType.BlockPropertyValue<?, ?, ?>> blockPropertyValues = context.getResult(2);
Expand All @@ -35,9 +35,9 @@ public void prepareCommandTree(CommandTree tree) {
context.addError("%" + TrKeys.M_COMMANDS_BLOCKSTATE_INVALIDSTATE, blockType.getIdentifier() + blockPropertyValues.toString());
return context.fail();
}
player.getDimension().setBlockState(pos, blockState);
entity.getDimension().setBlockState(pos, blockState);
context.addOutput(TrKeys.M_COMMANDS_SETBLOCK_SUCCESS);
return context.success();
}, SenderType.PLAYER);
}, SenderType.ENTITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.key("tps")
.optional()
.exec((context, player) -> {
player.sendText(TextFormat.GREEN + "TPS: " + player.getLocation().dimension().getWorld().getTPS());
.exec(context -> {
context.getSender().sendText(TextFormat.GREEN + "TPS: " + context.getSender().getCmdExecuteLocation().dimension().getWorld().getTPS());
return context.success();
}, SenderType.PLAYER)
})
.root()
.key("mspt")
.exec((context, player) -> {
player.sendText(TextFormat.GREEN + "MSPT: " + player.getLocation().dimension().getWorld().getMSPT());
.exec(context -> {
context.getSender().sendText(TextFormat.GREEN + "MSPT: " + context.getSender().getCmdExecuteLocation().dimension().getWorld().getMSPT());
return context.success();
}, SenderType.PLAYER);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ public WeatherCommand() {
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.enums("weather", Weather.class)
.exec((context, player) -> {
.exec(context -> {
Weather weather = Weather.valueOf(((String)context.getResult(0)).toUpperCase());
if (weather == Weather.CLEAR) {
player.getWorld().clearWeather();
context.getSender().getCmdExecuteLocation().dimension().getWorld().clearWeather();
} else {
player.getWorld().addWeather(weather);
context.getSender().getCmdExecuteLocation().dimension().getWorld().addWeather(weather);
}
context.addOutput(switch (weather) {
case CLEAR -> TrKeys.M_COMMANDS_WEATHER_CLEAR;
case RAIN -> TrKeys.M_COMMANDS_WEATHER_RAIN;
case THUNDER -> TrKeys.M_COMMANDS_WEATHER_THUNDER;
});
return context.success();
}, SenderType.PLAYER)
})
.root()
.key("query")
.exec((context, player) -> {
var weathers = player.getWorld().getWeathers();
.exec(context -> {
var weathers = context.getSender().getCmdExecuteLocation().dimension().getWorld().getWeathers();
if (weathers.contains(Weather.CLEAR)) {
player.sendTr(TrKeys.M_COMMANDS_WEATHER_QUERY, "clear");
context.getSender().sendTr(TrKeys.M_COMMANDS_WEATHER_QUERY, "clear");
return context.success();
}
if (weathers.contains(Weather.THUNDER)) {
player.sendTr(TrKeys.M_COMMANDS_WEATHER_QUERY, "rain and thunder");
context.getSender().sendTr(TrKeys.M_COMMANDS_WEATHER_QUERY, "rain and thunder");
return context.success();
}
player.sendTr(TrKeys.M_COMMANDS_WEATHER_QUERY, "rain");
context.getSender().sendTr(TrKeys.M_COMMANDS_WEATHER_QUERY, "rain");
return context.success();
}, SenderType.PLAYER);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void prepareCommandTree(CommandTree tree) {
.str("world")
.intNum("dimId")
.optional()
.exec((context, player) -> {
.exec((context, entity) -> {
String worldName = context.getResult(1);
int dimId = context.getResult(2);
var world = Server.getInstance().getWorldPool().getWorld(worldName);
Expand All @@ -59,9 +59,9 @@ public void prepareCommandTree(CommandTree tree) {
return context.fail();
}

player.teleport(new Location3f(0, 64, 0, dim));
entity.teleport(new Location3f(0, 64, 0, dim));
context.addOutput(TrKeys.A_COMMAND_WORLD_SUCCESS, worldName, dimId);
return context.success();
}, SenderType.PLAYER);
}, SenderType.ENTITY);
}
}

0 comments on commit e59aed8

Please sign in to comment.