Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOVA-Commands Implementation #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 cpw.mods.fml.common.event.FMLPreInitializationEvent;
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.Mod;
import nova.core.nativewrapper.NativeManager;
import nova.core.wrapper.mc.forge.v17.launcher.ForgeLoadable;
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 ForgeLoadable {

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);

this.events.on(ServerEvent.Start.class).bind(this::serverStarting);
}

@Override
public void preInit(FMLPreInitializationEvent evt) {
this.commands.init();
}

@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)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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 cpw.mods.fml.common.event.FMLPreInitializationEvent;
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.nativewrapper.NativeConverter;
import nova.core.wrapper.mc.forge.v17.launcher.ForgeLoadable;
import nova.internal.core.Game;

/**
* @author ExE Boss
*/
public class CommandConverter implements NativeConverter<Command, ICommand>, ForgeLoadable {

/**
* 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(FMLPreInitializationEvent event) {
//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);
}
}
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));
}
}
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;
}
}
Loading