diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/re/jag/parquet/Parquet.java | 6 | ||||
-rw-r--r-- | src/main/java/re/jag/parquet/commands/Rename.java | 48 |
2 files changed, 50 insertions, 4 deletions
diff --git a/src/main/java/re/jag/parquet/Parquet.java b/src/main/java/re/jag/parquet/Parquet.java index 06bf110..e031f29 100644 --- a/src/main/java/re/jag/parquet/Parquet.java +++ b/src/main/java/re/jag/parquet/Parquet.java @@ -13,10 +13,7 @@ import net.minecraft.item.DyeItem; import net.minecraft.item.Items; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.util.DyeColor; -import re.jag.parquet.commands.Calculator; -import re.jag.parquet.commands.CameraMode; -import re.jag.parquet.commands.Savedata; -import re.jag.parquet.commands.TimeDIff; +import re.jag.parquet.commands.*; public class Parquet implements ModInitializer { public static final Logger LOG = LogManager.getLogger(); @@ -31,6 +28,7 @@ public class Parquet implements ModInitializer { CameraMode.register(dispatcher); Calculator.register(dispatcher); TimeDIff.register(dispatcher); + Rename.register(dispatcher); LOG.info("[PQ] Registered commands"); } diff --git a/src/main/java/re/jag/parquet/commands/Rename.java b/src/main/java/re/jag/parquet/commands/Rename.java new file mode 100644 index 0000000..79d6040 --- /dev/null +++ b/src/main/java/re/jag/parquet/commands/Rename.java @@ -0,0 +1,48 @@ +package re.jag.parquet.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.item.ItemStack; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.LiteralText; +import net.minecraft.util.Hand; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +public class Rename { + public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { + LiteralArgumentBuilder<ServerCommandSource> rename = literal("rename"). + requires((s) -> can_execute(s)). + then( argument("name", StringArgumentType.string()). + executes((c) -> rename(c.getSource(), StringArgumentType.getString(c, "name"))) + ); + + dispatcher.register(rename); + } + + private static int rename (ServerCommandSource _source, String _name) throws CommandSyntaxException { + + ItemStack stack = _source.getPlayer().getStackInHand(Hand.MAIN_HAND); + if (stack.isEmpty()) { + _source.sendError(new LiteralText("Your hand is empty")); + return 0; + } + + stack.setCustomName(new LiteralText(_name)); + return 1; + } + + private static boolean can_execute (ServerCommandSource _source) { + if(_source.getMinecraftServer().getDefaultGameMode().isCreative()) + return true; + + try { + return _source.getPlayer().interactionManager.getGameMode().isCreative(); + } catch (CommandSyntaxException e) { + return false; + } + } +} |