Skip to content

Commit

Permalink
NOVA-Commands Minecraft 1.8 Wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Jan 14, 2017
1 parent b7845de commit 2490f9d
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 0 deletions.
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_8.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_8.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)));
}
}
}
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_8.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_8.wrapper.commands.backward.BWCommand;
import nova.commands.wrapper.mc.forge.v1_8.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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_8.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.v18.util.WrapUtility;
import nova.internal.core.Game;
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, 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.getErrorObjects());
}
}

@Override
@SuppressWarnings("unchecked")
public List<String> getAutocompleteList(Optional<Player> player, String[] args, Optional<Vector3D> pos) {
return this.wrapped.addTabCompletionOptions(WrapUtility.getMCPlayer(player), args, pos.isPresent() ? Game.natives().toNative(pos.get()) : null);
}

@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,79 @@
/*
* 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_8.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 net.minecraft.util.BlockPos;
import nova.commands.Command;
import nova.core.wrapper.mc.forge.v18.util.WrapUtility;
import nova.internal.core.Game;

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(), 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, BlockPos pos) {
return this.wrapped.getAutocompleteList(sender instanceof EntityPlayer ? WrapUtility.getNovaPlayer((EntityPlayer) sender) : Optional.empty(), args, pos == null ? Optional.empty() : Optional.of(Game.natives().toNova(pos)));
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender sender) {
return true;
}
}

0 comments on commit 2490f9d

Please sign in to comment.