From d9d9bd9824bae6b4d1d831b461109b8cd164305c Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Fri, 13 Jan 2017 23:18:57 +0100 Subject: [PATCH] NOVA-Commands Minecraft 1.7.10 Wrappers --- .../mc/forge/v1_7_10/launch/NovaCommands.java | 70 +++++++++++ .../wrapper/commands/CommandConverter.java | 112 ++++++++++++++++++ .../wrapper/commands/backward/BWCommand.java | 64 ++++++++++ .../wrapper/commands/forward/FWCommand.java | 77 ++++++++++++ 4 files changed, 323 insertions(+) create mode 100644 minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/launch/NovaCommands.java create mode 100644 minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/CommandConverter.java create mode 100644 minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/backward/BWCommand.java create mode 100644 minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/forward/FWCommand.java diff --git a/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/launch/NovaCommands.java b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/launch/NovaCommands.java new file mode 100644 index 0000000..fac1876 --- /dev/null +++ b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/launch/NovaCommands.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ + +package nova.commands.wrapper.mc.forge.v1_7_10.launch; + +import net.minecraft.command.CommandHandler; +import net.minecraft.command.ICommand; +import net.minecraft.command.ICommandManager; +import net.minecraft.server.MinecraftServer; +import nova.commands.CommandManager; +import nova.commands.wrapper.mc.forge.v1_7_10.wrapper.commands.CommandConverter; +import nova.core.event.ServerEvent; +import nova.core.event.bus.GlobalEvents; +import nova.core.loader.Loadable; +import nova.core.loader.Mod; +import nova.core.nativewrapper.NativeManager; +import nova.internal.commands.depmodules.CommandsModule; + +/** + * @author ExE Boss + */ +@Mod(id = "nova-commands-wrapper", name = "NOVA Commands Wrapper", version = "0.0.1", novaVersion = "0.1.0", modules = { CommandsModule.class }) +public class NovaCommands implements Loadable { + + private final NativeManager natives; + private final CommandManager commands; + private final GlobalEvents events; + + private final CommandConverter converter; + + public NovaCommands(CommandManager commandManager, GlobalEvents events, NativeManager natives) { + this.commands = commandManager; + this.events = events; + this.natives = natives; + + this.converter = new CommandConverter(); + this.natives.registerConverter(this.converter); + } + + @Override + public void preInit() { + this.events.on(ServerEvent.Start.class).bind(this::serverStarting); + } + + @SuppressWarnings("unchecked") + public void serverStarting(ServerEvent.Start evt) { + ICommandManager mcManager = MinecraftServer.getServer().getCommandManager(); + mcManager.getCommands().values().stream().filter(cmd -> cmd instanceof ICommand).forEach(cmd -> this.converter.registerMinecraftCommand((ICommand) cmd)); + if (mcManager instanceof CommandHandler) { + this.commands.registry.forEach(command -> ((CommandHandler) mcManager).registerCommand(this.natives.toNative(command))); + } + } +} diff --git a/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/CommandConverter.java b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/CommandConverter.java new file mode 100644 index 0000000..150c5ea --- /dev/null +++ b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/CommandConverter.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ + +package nova.commands.wrapper.mc.forge.v1_7_10.wrapper.commands; + +import com.google.common.collect.HashBiMap; +import net.minecraft.command.ICommand; +import nova.commands.Command; +import nova.commands.event.CommandEvent; +import nova.commands.wrapper.mc.forge.v1_7_10.wrapper.commands.backward.BWCommand; +import nova.commands.wrapper.mc.forge.v1_7_10.wrapper.commands.forward.FWCommand; +import nova.core.loader.Loadable; +import nova.core.nativewrapper.NativeConverter; +import nova.internal.core.Game; + +/** + * @author ExE Boss + */ +public class CommandConverter implements NativeConverter, Loadable { + + /** + * A map of all items registered + */ + private final HashBiMap map = HashBiMap.create(); + + @Override + public Class getNovaSide() { + return Command.class; + } + + @Override + public Class getNativeSide() { + return ICommand.class; + } + + @Override + public Command toNova(ICommand command) { + if (command == null) { + return null; + } + + if (command instanceof FWCommand) { + return ((FWCommand) command).wrapped; + } else { + if (map.containsValue(command)) { + return map.inverse().get(command); + } else { + BWCommand wrapper = new BWCommand(command); + map.put(wrapper, command); + return wrapper; + } + } + } + + @Override + public ICommand toNative(Command command) { + if (command == null) { + return null; + } + + if (command instanceof BWCommand) { + return ((BWCommand) command).wrapped; + } else { + if (map.containsKey(command)) { + return map.get(command); + } else { + FWCommand wrapper = new FWCommand(command); + map.put(command, wrapper); + return wrapper; + } + } + } + + @Override + public void preInit() { + //NOTE: There should NEVER be command already registered in preInit() stage of a NativeConverter. + Game.events().on(CommandEvent.Register.class).bind(evt -> registerNovaCommand(evt.command)); + } + + public void registerMinecraftCommand(ICommand command) { + if (map.containsValue(command)) + return; + + BWCommand wrapper = new BWCommand(command); + map.put(wrapper, command); + } + + private void registerNovaCommand(Command command) { + if (map.containsKey(command)) + return; + + FWCommand wrapper = new FWCommand(command); + map.put(command, wrapper); + } +} diff --git a/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/backward/BWCommand.java b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/backward/BWCommand.java new file mode 100644 index 0000000..c805fd4 --- /dev/null +++ b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/backward/BWCommand.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ + +package nova.commands.wrapper.mc.forge.v1_7_10.wrapper.commands.backward; + +import net.minecraft.command.ICommand; +import nova.commands.Command; +import nova.commands.exception.CommandException; +import nova.core.entity.component.Player; +import nova.core.wrapper.mc.forge.v17.util.WrapUtility; +import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; + +import java.util.List; +import java.util.Optional; + +/** + * @author ExE Boss + */ +public class BWCommand extends Command { + + public final ICommand wrapped; + + public BWCommand(ICommand wrapped) { + super(wrapped.getCommandName()); + this.wrapped = wrapped; + } + + @Override + public void handle(Optional player, String... args) throws CommandException { + try { + this.wrapped.processCommand(WrapUtility.getMCPlayer(player), args); + } catch (net.minecraft.command.CommandException ex) { + throw new CommandException(ex.getMessage(), ex, ex.getErrorOjbects()); + } + } + + @Override + @SuppressWarnings("unchecked") + public List getAutocompleteList(Optional player, String[] args, Optional pos) { + return this.wrapped.addTabCompletionOptions(WrapUtility.getMCPlayer(player), args); + } + + @Override + public String getCommandUsage(Optional player) { + return wrapped.getCommandUsage(WrapUtility.getMCPlayer(player)); + } +} diff --git a/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/forward/FWCommand.java b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/forward/FWCommand.java new file mode 100644 index 0000000..75c762f --- /dev/null +++ b/minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/forward/FWCommand.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.commands.wrapper.mc.forge.v1_7_10.wrapper.commands.forward; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import nova.commands.Command; +import nova.core.wrapper.mc.forge.v17.util.WrapUtility; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +/** + * @author ExE Boss + */ +public class FWCommand extends CommandBase { + + public final Command wrapped; + + public FWCommand(Command wrapped) { + this.wrapped = wrapped; + } + + @Override + public String getCommandName() { + return wrapped.getCommandName(); + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return wrapped.getCommandUsage(sender instanceof EntityPlayer ? WrapUtility.getNovaPlayer((EntityPlayer) sender) : Optional.empty()); + } + + @Override + public List getCommandAliases() { + return Collections.emptyList(); + } + + @Override + public void processCommand(ICommandSender sender, String[] args) throws CommandException { + try { + this.wrapped.handle(sender instanceof EntityPlayer ? WrapUtility.getNovaPlayer((EntityPlayer) sender) : Optional.empty(), args); + } catch (nova.commands.exception.CommandException ex) { + throw (CommandException) new CommandException(ex.getMessage(), ex.getArguments().orElse(new Object[0])).initCause(ex); + } + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] args) { + return this.wrapped.getAutocompleteList(sender instanceof EntityPlayer ? WrapUtility.getNovaPlayer((EntityPlayer) sender) : Optional.empty(), args, Optional.empty()); + } + + @Override + public boolean canCommandSenderUseCommand(ICommandSender sender) { + return true; + } +}