-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
minecraft/1.7/src/main/java/nova/commands/wrapper/mc/forge/v1_7_10/launch/NovaCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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))); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...c/main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/CommandConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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<Command, ICommand>, Loadable { | ||
|
||
/** | ||
* A map of all items registered | ||
*/ | ||
private final HashBiMap<Command, ICommand> map = HashBiMap.create(); | ||
|
||
@Override | ||
public Class<Command> getNovaSide() { | ||
return Command.class; | ||
} | ||
|
||
@Override | ||
public Class<ICommand> 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); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/backward/BWCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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> player, boolean prejoined, 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<String> getAutocompleteList(Optional<Player> player, boolean prejoined, String[] args, Optional<Vector3D> pos) { | ||
return this.wrapped.addTabCompletionOptions(WrapUtility.getMCPlayer(player), args); | ||
} | ||
|
||
@Override | ||
public String getCommandUsage(Optional<Player> player) { | ||
return wrapped.getCommandUsage(WrapUtility.getMCPlayer(player)); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
.../main/java/nova/commands/wrapper/mc/forge/v1_7_10/wrapper/commands/forward/FWCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
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<String> 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(), false, args); | ||
} catch (nova.commands.exception.CommandException ex) { | ||
throw (CommandException) new CommandException(ex.getMessage(), ex.getArguments().orElse(new Object[0])).initCause(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> addTabCompletionOptions(ICommandSender sender, String[] args) { | ||
return this.wrapped.getAutocompleteList(sender instanceof EntityPlayer ? WrapUtility.getNovaPlayer((EntityPlayer) sender) : Optional.empty(), false, args, Optional.empty()); | ||
} | ||
|
||
@Override | ||
public boolean canCommandSenderUseCommand(ICommandSender sender) { | ||
return true; | ||
} | ||
} |