animalrpfabric/src/main/java/ovh/sad/animalrp/commands/TfCommand.java
sophie 2f5084010a
All checks were successful
build / build (21) (push) Successful in 1m31s
simple-ish database + more improvements
2024-09-13 20:58:26 +03:00

74 lines
No EOL
3.7 KiB
Java

package ovh.sad.animalrp.commands;
import java.util.Map.Entry;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.entity.Entity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import ovh.sad.animalrp.AnimalRP;
import ovh.sad.animalrp.animals.Animal;
import ovh.sad.animalrp.util.HashmapStore;
public class TfCommand {
public void Command(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess,
CommandManager.RegistrationEnvironment environment) {
dispatcher.register(CommandManager.literal("tf")
.then(CommandManager.argument("animal", StringArgumentType.string())
.executes(context -> {
final Entity entity = context.getSource().getEntity();
final String animalString = StringArgumentType.getString(context, "animal");
System.out.println(animalString);
System.out.println(animalString.length());
Animal animal = AnimalRP.animals.get(animalString);
if (animalString.equals("off")) {
if (AnimalRP.users.get(entity.getUuid()) == null) {
context.getSource().sendFeedback(
() -> Text.literal("You do not have a animal set."), false);
return 0;
}
AnimalRP.users.remove(entity.getUuid());
context.getSource().sendFeedback(
() -> Text.literal("You no longer have a animal set."), false);
return 0;
}
if (animal == null) {
classicError(context.getSource());
return 0;
}
AnimalRP.users.put(entity.getUuid(), animal);
context.getSource()
.sendFeedback(
() -> Text.literal("You are now an " + animalString + "! ")
.append(Text.literal(animal.catchphrase)
.formatted(Formatting.ITALIC).withColor(
Integer.parseInt(animal.color.substring(1),
16))),
false);
HashmapStore.save("users.json", AnimalRP.users);
return 1;
})));
}
void classicError(ServerCommandSource source) {
MutableText options = Text.literal("Your options are: ");
for (Entry<String, Animal> entry : AnimalRP.animals.entrySet()) {
options.append(Text.literal(entry.getKey() + " ")
.withColor(Integer.parseInt(entry.getValue().color.substring(1), 16)));
}
source.sendFeedback(() -> Text.literal("Invalid animal!").withColor(16711680), false);
source.sendFeedback(() -> options, false);
source.sendFeedback(() -> Text.literal("Use /tf off to disable the changes."), false);
}
}