Skip to content

Commit

Permalink
Added /weather and /spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Sep 26, 2023
1 parent 1107dbc commit f90d2ad
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 64 deletions.
1 change: 0 additions & 1 deletion src/main/java/net/pedroricardo/commander/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// TODO: ChunkCoordinateArgumentType
// TODO: ItemStackArgumentType
// TODO: WorldFeatureArgumentType
// TODO: WeatherArgumentType

public class Commander implements ModInitializer {
public static final String MOD_ID = "commander";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.core.net.command.CommandHandler;
import net.minecraft.core.net.command.CommandSender;
import net.minecraft.core.util.phys.Vec3d;
import net.minecraft.core.world.Dimension;
import net.minecraft.core.world.World;
import net.pedroricardo.commander.Commander;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -94,6 +95,33 @@ public World getWorld(int dimension) {
return this.mc.theWorld;
}

public void movePlayerToDimension(EntityPlayer player, int dimension) {
Dimension lastDim = Dimension.getDimensionList().get(player.dimension);
Dimension newDim = Dimension.getDimensionList().get(dimension);
System.out.println("Switching to dimension \"" + newDim.getTranslatedName() + "\"!!");
player.dimension = dimension;
this.mc.theWorld.setEntityDead(player);
this.mc.thePlayer.removed = false;
double x = player.x;
double y = player.y + 64;
double z = player.z;
player.moveTo(x *= Dimension.getCoordScale(lastDim, newDim), y, z *= Dimension.getCoordScale(lastDim, newDim), player.yRot, player.xRot);
if (player.isAlive()) {
this.mc.theWorld.updateEntityWithOptionalForce(player, false);
}
World world = new World(this.mc.theWorld, newDim);
if (newDim == lastDim.homeDim) {
this.mc.changeWorld(world, "Leaving " + lastDim.getTranslatedName(), player);
} else {
this.mc.changeWorld(world, "Entering " + newDim.getTranslatedName(), player);
}
player.world = this.mc.theWorld;
if (player.isAlive()) {
player.moveTo(x, y, z, player.yRot, player.xRot);
this.mc.theWorld.updateEntityWithOptionalForce(player, false);
}
}

@Override
public @Deprecated CommandHandler getCommandHandler() {
return this.mc.commandHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class CommanderCommandManager {
SetSpawnCommand.register(DISPATCHER);
TimeCommand.register(DISPATCHER);
GameModeCommand.register(DISPATCHER);
WeatherCommand.register(DISPATCHER);
SpawnCommand.register(DISPATCHER);
// GiveCommand.register(DISPATCHER);
// HelpCommand.register(DISPATCHER);
// WeatherCommand.register(DISPATCHER);
// GamemodeCommand.register(DISPATCHER);
// GenerateCommand.register(DISPATCHER);
// ChunkCommand.register(DISPATCHER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ default Collection<String> getEntitySuggestions() {

World getWorld(int dimension);

void movePlayerToDimension(EntityPlayer player, int dimension);

@Deprecated CommandHandler getCommandHandler();

@Deprecated CommandSender getCommandSender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public World getWorld(int dimension) {
return this.server.getWorldManager(dimension);
}

@Override
public void movePlayerToDimension(EntityPlayer player, int dimension) {
if (player instanceof EntityPlayerMP) this.server.configManager.sendPlayerToOtherDimension((EntityPlayerMP) player, dimension);
throw new IllegalStateException("Player is not an instance of EntityPlayerMP");
}

@Override
public @Deprecated CommandHandler getCommandHandler() {
return this.server.serverCommandHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public World getWorld(int dimension) {
return this.server.getWorldManager(dimension);
}

@Override
public void movePlayerToDimension(EntityPlayer player, int dimension) {
if (player instanceof EntityPlayerMP) this.server.configManager.sendPlayerToOtherDimension((EntityPlayerMP) player, dimension);
throw new IllegalStateException("Player is not an instance of EntityPlayerMP");
}

@Override
public @Deprecated CommandHandler getCommandHandler() {
return this.server.serverCommandHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.pedroricardo.commander.content.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.core.lang.I18n;
import net.minecraft.core.player.gamemode.Gamemode;
import net.minecraft.core.world.weather.Weather;
import net.pedroricardo.commander.CommanderHelper;

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class WeatherArgumentType implements ArgumentType<Weather> {
private static final Collection<String> EXAMPLES = Arrays.asList(Weather.overworldClear.languageKey, Weather.overworldFog.languageKey);

public static ArgumentType<Weather> weather() {
return new WeatherArgumentType();
}

@Override
public Weather parse(StringReader reader) throws CommandSyntaxException {
final String string = reader.readString();

for (Weather weather : Weather.weatherList) {
if (weather == null) continue;
if (CommanderHelper.matchesKeyString(weather.languageKey, string)) {
return weather;
}
}
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.weather.invalid_weather"));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
String remaining = builder.getRemainingLowerCase();
for (Weather weather : Weather.weatherList) {
if (weather == null) continue;
Optional<String> optional = CommanderHelper.getStringToSuggest(weather.languageKey, remaining);
optional.ifPresent(builder::suggest);
}
return builder.buildFuture();
}

@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
.then(RequiredArgumentBuilder.argument("entities", EntityArgumentType.players())
.then(RequiredArgumentBuilder.argument("achievement", AchievementArgumentType.achievement())
.executes(c -> {
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
List<? extends Entity> entities = c.getArgument("entities", EntitySelector.class).get((CommanderCommandSource) c.getSource());
Achievement achievement = c.getArgument("achievement", Achievement.class);

Expand All @@ -54,12 +55,13 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
}
}

sendContextualMessage((CommanderCommandSource) c.getSource(), entities, achievement);
sendContextualMessage(source, entities, achievement);

return Command.SINGLE_SUCCESS;
}))
.then(LiteralArgumentBuilder.literal("*")
.executes(c -> {
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
List<? extends Entity> entities = c.getArgument("entities", EntitySelector.class).get((CommanderCommandSource) c.getSource());

if (entities.size() == 0) {
Expand All @@ -79,7 +81,7 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
}
}

sendWildcardContextualMessage(((CommanderCommandSource)c.getSource()), entities);
sendWildcardContextualMessage(source, entities);

return Command.SINGLE_SUCCESS;
}))))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("clear")
.requires(source -> ((CommanderCommandSource)source).hasAdmin())
.executes(c -> {
if (((CommanderCommandSource)c.getSource()).getSender() == null) throw CommanderExceptions.notInWorld().create();

CommanderCommandSource source = (CommanderCommandSource) c.getSource();
EntityPlayer sender = ((CommanderCommandSource)c.getSource()).getSender();

if (sender == null) throw CommanderExceptions.notInWorld().create();

int itemsCleared = clearItemsFromEntity(sender);

((CommanderCommandSource) c.getSource()).sendMessage(getMessage(itemsCleared, Collections.singletonList(sender)));
source.sendMessage(getMessage(itemsCleared, Collections.singletonList(sender)));

return Command.SINGLE_SUCCESS;
})
.then(RequiredArgumentBuilder.argument("players", EntityArgumentType.players())
.executes(c -> {
List<? extends Entity> players = c.getArgument("players", EntitySelector.class).get(((CommanderCommandSource)c.getSource()));
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
List<? extends Entity> players = c.getArgument("players", EntitySelector.class).get(source);

int itemsCleared = 0;

for (Entity player : players) {
itemsCleared += clearItemsFromEntity((EntityPlayer)player);
}

((CommanderCommandSource) c.getSource()).sendMessage(getMessage(itemsCleared, players));
source.sendMessage(getMessage(itemsCleared, players));

return Command.SINGLE_SUCCESS;
})));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,37 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
.requires(source -> ((CommanderCommandSource)source).hasAdmin())
.then(RequiredArgumentBuilder.argument("gamemode", GameModeArgumentType.gameMode())
.executes(c -> {
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
Gamemode gameMode = c.getArgument("gamemode", Gamemode.class);

if (((CommanderCommandSource)c.getSource()).getSender() == null) {
if (source.getSender() == null) {
throw CommanderExceptions.notInWorld().create();
}
((CommanderCommandSource)c.getSource()).getSender().setGamemode(gameMode);
((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.gamemode.success_self", I18n.getInstance().translateKey(gameMode.languageKey + ".name")));

source.getSender().setGamemode(gameMode);
source.sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.gamemode.success_self", I18n.getInstance().translateKey(gameMode.languageKey + ".name")));
return Command.SINGLE_SUCCESS;
})
.then(RequiredArgumentBuilder.argument("targets", EntityArgumentType.players())
.executes(c -> {
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
Gamemode gameMode = c.getArgument("gamemode", Gamemode.class);
EntitySelector entitySelector = c.getArgument("targets", EntitySelector.class);

List<? extends Entity> entities = entitySelector.get((CommanderCommandSource)c.getSource());

for (Entity entity : entities) {
((EntityPlayer)entity).setGamemode(gameMode);
if (entity != ((CommanderCommandSource)c.getSource()).getSender()) {
((CommanderCommandSource) c.getSource()).sendMessageToPlayer((EntityPlayer) entity, I18n.getInstance().translateKey("commands.commander.gamemode.success_receiver"));
if (entity != source.getSender()) {
source.sendMessageToPlayer((EntityPlayer) entity, I18n.getInstance().translateKey("commands.commander.gamemode.success_receiver"));
}
}

if (entities.size() == 1) {
if (entities.get(0) == ((CommanderCommandSource)c.getSource()).getSender()) {
((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.gamemode.success_self", I18n.getInstance().translateKey(gameMode.languageKey + ".name")));
if (entities.get(0) == source.getSender()) {
source.sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.gamemode.success_self", I18n.getInstance().translateKey(gameMode.languageKey + ".name")));
} else {
((CommanderCommandSource) c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.gamemode.success_other", CommanderHelper.getEntityName(entities.get(0)), I18n.getInstance().translateKey(gameMode.languageKey + ".name")));
source.sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.gamemode.success_other", CommanderHelper.getEntityName(entities.get(0)), I18n.getInstance().translateKey(gameMode.languageKey + ".name")));
}
}
return Command.SINGLE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
dispatcher.register((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("kill")
.requires(source -> ((CommanderCommandSource)source).hasAdmin())
.executes(c -> {
EntityPlayer sender = ((CommanderCommandSource)c.getSource()).getSender();
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
EntityPlayer sender = source.getSender();

if (sender == null) throw CommanderExceptions.notInWorld().create();

sender.killPlayer();

((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.single_entity", sender.getDisplayName()));
source.sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.single_entity", sender.getDisplayName()));

return Command.SINGLE_SUCCESS;
})
.then(RequiredArgumentBuilder.argument("entities", EntityArgumentType.entities())
.executes(c -> {
CommanderCommandSource source = (CommanderCommandSource) c.getSource();
EntitySelector entitySelector = c.getArgument("entities", EntitySelector.class);
CopyOnWriteArrayList<? extends Entity> entities = new CopyOnWriteArrayList<>(entitySelector.get((CommanderCommandSource)c.getSource()));
CopyOnWriteArrayList<? extends Entity> entities = new CopyOnWriteArrayList<>(entitySelector.get(source));

int entityCount = entities.size();

if (entityCount == 0) ((CommanderCommandSource)c.getSource()).sendMessage("§e" + I18n.getInstance().translateKey("commands.commander.kill.failure"));
else if (entityCount == 1) ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.single_entity", CommanderHelper.getEntityName(entities.get(0))));
else ((CommanderCommandSource)c.getSource()).sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.multiple_entities", entityCount));
if (entityCount == 0) source.sendMessage("§e" + I18n.getInstance().translateKey("commands.commander.kill.failure"));
else if (entityCount == 1) source.sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.single_entity", CommanderHelper.getEntityName(entities.get(0))));
else source.sendMessage(I18n.getInstance().translateKeyAndFormat("commands.commander.kill.multiple_entities", entityCount));

for (Entity entity : entities) {
if (entity instanceof EntityPlayer) {
Expand Down
Loading

0 comments on commit f90d2ad

Please sign in to comment.