Changed connector to have an implementation interface

This commit is contained in:
Sekwah 2018-07-05 03:17:17 +01:00 committed by Sekwah
parent 6d887c5b37
commit fafc2d20d8
20 changed files with 293 additions and 142 deletions

View File

@ -18,8 +18,8 @@ import com.sekwah.advancedportals.core.data.DataStorage;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.util.InfoLogger;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.coreconnector.ConnectorDataCollector;
import com.sekwah.advancedportals.coreconnector.command.CommandRegister;
import com.sekwah.advancedportals.coreconnector.info.DataCollector;
import java.io.File;
@ -30,7 +30,7 @@ public class AdvancedPortalsCore {
private final CommandRegister commandRegister;
private final InfoLogger infoLogger;
private final int mcMinorVer;
private final ConnectorDataCollector dataCollector;
private final DataCollector dataCollector;
private Injector injector = Guice.createInjector(new RepositoryModule(this));
@ -61,7 +61,7 @@ public class AdvancedPortalsCore {
* @param mcVer Minecraft version e.g. 1.12.2
*/
public AdvancedPortalsCore(File dataStorageLoc, InfoLogger infoLogger, CommandRegister commandRegister,
ConnectorDataCollector dataCollector, int[] mcVer) {
DataCollector dataCollector, int[] mcVer) {
this.dataStorage = new DataStorage(dataStorageLoc);
this.infoLogger = infoLogger;
instance = this;
@ -69,6 +69,9 @@ public class AdvancedPortalsCore {
this.dataCollector = dataCollector;
this.mcMinorVer = this.checkMcVer(mcVer);
System.out.println("Core");
this.onEnable();
}
@ -196,7 +199,7 @@ public class AdvancedPortalsCore {
return this.infoLogger;
}
public ConnectorDataCollector getDataCollector() {
public DataCollector getDataCollector() {
return this.dataCollector;
}

View File

@ -1,6 +1,5 @@
package com.sekwah.advancedportals.core.api.services;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.api.destination.Destination;
import com.sekwah.advancedportals.core.entities.DataTag;
import com.sekwah.advancedportals.core.entities.PlayerLocation;

View File

@ -1,7 +1,6 @@
package com.sekwah.advancedportals.core.api.services;
import com.google.common.collect.ImmutableList;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.api.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.entities.DataTag;
import com.sekwah.advancedportals.core.entities.PlayerLocation;

View File

@ -14,7 +14,6 @@ public class TagHandler {
*
* @param player if null then created by the server or a plugin
* @param argData
* @throws PortalException message given is the reason the portal (or destination) cannot be made
*/
void created(T target, PlayerContainer player, String argData);
@ -23,7 +22,6 @@ public class TagHandler {
*
* @param player if null then removed by the server or a plugin
* @param argData
* @throws PortalException message given is the reason the portal cant be removed
*/
void destroyed(T target, PlayerContainer player, String argData);

View File

@ -16,8 +16,8 @@ public class ReloadSubCommand implements SubCommand {
@Override
public void onCommand(CommandSenderContainer sender, String[] args) {
portalsCore.loadPortalConfig();
portalsCore.getPortalServices().loadPortals(portalsCore);
portalsCore.getDestinationServices().loadDestinations(portalsCore);
portalsCore.getPortalServices().loadPortals();
portalsCore.getDestinationServices().loadDestinations();
sender.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translateColor("command.reload.reloaded"));
}

View File

@ -16,6 +16,7 @@ public class RepositoryModule extends AbstractModule {
@Override
protected void configure() {
System.out.println("Configured");
bind(PortalRepository.class).to(PortalRepositoryImpl.class).in(Scopes.SINGLETON);
bind(DestinationRepository.class).to(DestinationRepositoryImpl.class).in(Scopes.SINGLETON);
bind(PortalTempDataRepository.class).to(PortalTempDataRepositoryImpl.class).in(Scopes.SINGLETON);
@ -24,6 +25,7 @@ public class RepositoryModule extends AbstractModule {
@Provides
AdvancedPortalsCore providePortalsCore() {
System.out.println("TEST");
return this.portalsCore;
}
}

View File

@ -1,30 +1,14 @@
package com.sekwah.advancedportals.coreconnector.command;
import com.sekwah.advancedportals.core.commands.CommandTemplate;
import com.sekwah.advancedportals.coreconnector.container.CommandSenderContainer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import java.util.List;
public abstract class CommandHandler {
public class CommandHandler implements CommandExecutor, TabCompleter {
private final CommandTemplate commandExecutor;
protected final CommandTemplate commandExecutor;
public CommandHandler(CommandTemplate commandExecutor) {
this.commandExecutor = commandExecutor;
}
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
this.commandExecutor.onCommand(new CommandSenderContainer(commandSender), command.getName(), args);
return true;
}
@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
return this.commandExecutor.onTabComplete(new CommandSenderContainer(commandSender), args);
}
}

View File

@ -1,25 +1,14 @@
package com.sekwah.advancedportals.coreconnector.command;
import com.sekwah.advancedportals.core.commands.CommandTemplate;
import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin;
/**
* Register the CommandTemplate files to the appropriate system
*/
public class CommandRegister {
private final AdvancedPortalsPlugin plugin;
public CommandRegister(AdvancedPortalsPlugin plugin) {
this.plugin = plugin;
}
public interface CommandRegister {
/**
* Registers the command to the appropriate system
* @param commandName
* @param commandExecutor
*/
public void registerCommand(String commandName, CommandTemplate commandExecutor) {
this.plugin.getCommand(commandName).setExecutor(new CommandHandler(commandExecutor));
}
void registerCommand(String commandName, CommandTemplate commandExecutor);
}

View File

@ -1,35 +1,16 @@
package com.sekwah.advancedportals.coreconnector.container;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public interface CommandSenderContainer {
public class CommandSenderContainer {
void sendMessage(String message);
private final CommandSender sender;
public CommandSenderContainer(CommandSender commandSender) {
this.sender = commandSender;
}
public void sendMessage(String message) {
sender.sendMessage(message);
}
public boolean isOp() {
return sender.isOp();
}
boolean isOp();
/**
* @return null if there isnt a player e.g. the console
*/
public PlayerContainer getPlayerContainer() {
if (sender instanceof Player) {
return new PlayerContainer((Player) sender);
}
return null;
}
PlayerContainer getPlayerContainer();
boolean hasPermission(String permission);
public boolean hasPermission(String permission) {
return sender.hasPermission(permission);
}
}

View File

@ -8,60 +8,34 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.UUID;
/**
* Just a temporary container for whenever advanced portals needs to get data from a player
*/
public class PlayerContainer {
public interface PlayerContainer {
private final Player player;
UUID getUUID();
public PlayerContainer(Player player) {
this.player = player;
}
public void sendMessage(String message);
public UUID getUUID() {
return player.getUniqueId();
}
public void sendMessage(String message) {
player.sendMessage(message);
}
boolean isOp();
public boolean isOp() {
return this.player.isOp();
}
PlayerLocation getLoc();
public PlayerLocation getLoc() {
Location loc = this.player.getLocation();
return new PlayerLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ());
}
double getEyeHeight();
public double getEyeHeight() {
return 0;
}
void teleport(PlayerLocation location);
public void teleport(PlayerLocation location) {
this.player.teleport(new Location(Bukkit.getWorld(location.worldName), location.posX, location.posY, location.posZ));
}
boolean hasPermission(String permission);
public boolean hasPermission(String permission) {
return this.player.hasPermission(permission);
}
public WorldContainer getWorld() {
return new WorldContainer(this.player.getWorld());
}
WorldContainer getWorld();
/**
* @param blockPos
* @param material
*/
public void sendFakeBlock(PortalLocation blockPos, String material) {
}
void sendFakeBlock(PortalLocation blockPos, String material);
/**
* Only 1.12 and below supported
@ -69,9 +43,9 @@ public class PlayerContainer {
* @param material
* @param data
*/
public void sendFakeBlockWithData(PortalLocation blockPos, String material, byte data) {
void sendFakeBlockWithData(PortalLocation blockPos, String material, byte data);
}
void giveWool(String dyeColor, String itemName, String... itemDescription);
public void giveItem(String material, String itemName, String... itemDescription) {
ItemStack regionselector = new ItemStack(Material.getMaterial(material));

View File

@ -7,33 +7,13 @@ import org.bukkit.block.BlockFace;
import org.bukkit.material.Directional;
import org.bukkit.material.MaterialData;
public class WorldContainer {
public interface WorldContainer {
private final World world;
void setBlock(PortalLocation location, String material);
public WorldContainer(World world) {
this.world = world;
}
void setBlockData(PortalLocation location, byte data);
public void setBlock(PortalLocation location, String material) {
this.world.getBlockAt(location.posX, location.posY, location.posZ).setType(Material.getMaterial(material));
}
String getBlock(PortalLocation location);
public void setBlockData(PortalLocation location, byte data) {
MaterialData matData = world.getBlockAt(location.posX, location.posY, location.posZ).getState().getData();
if(matData instanceof Directional) {
System.out.println("IS DIRECTIONAL");
Directional dir = (Directional) world.getBlockAt(location.posX, location.posY, location.posZ).getState().getData();
dir.setFacingDirection(BlockFace.NORTH);
}
}
public String getBlock(PortalLocation location) {
return this.world.getBlockAt(location.posX, location.posY, location.posZ).getType().toString();
}
public byte getBlockData(PortalLocation location) {
return 0;
}
byte getBlockData(PortalLocation location);
}

View File

@ -0,0 +1,10 @@
package com.sekwah.advancedportals.coreconnector.info;
/**
* Gets info from the specific implementation
*/
public interface DataCollector {
boolean materialExists(String materialName);
}

View File

@ -0,0 +1,29 @@
package com.sekwah.advancedportals.coreconnector.spigot.command;
import com.sekwah.advancedportals.core.commands.CommandTemplate;
import com.sekwah.advancedportals.coreconnector.command.CommandHandler;
import com.sekwah.advancedportals.coreconnector.spigot.container.SpigotCommandSenderContainer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import java.util.List;
public class SpigotCommandHandler extends CommandHandler implements CommandExecutor, TabCompleter {
public SpigotCommandHandler(CommandTemplate commandExecutor) {
super(commandExecutor);
}
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
this.commandExecutor.onCommand(new SpigotCommandSenderContainer(commandSender), command.getName(), args);
return true;
}
@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
return this.commandExecutor.onTabComplete(new SpigotCommandSenderContainer(commandSender), args);
}
}

View File

@ -0,0 +1,26 @@
package com.sekwah.advancedportals.coreconnector.spigot.command;
import com.sekwah.advancedportals.core.commands.CommandTemplate;
import com.sekwah.advancedportals.coreconnector.command.CommandRegister;
import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin;
/**
* Register the CommandTemplate files to the appropriate system
*/
public class SpigotCommandRegister implements CommandRegister {
private final AdvancedPortalsPlugin plugin;
public SpigotCommandRegister(AdvancedPortalsPlugin plugin) {
this.plugin = plugin;
}
/**
* Registers the command to the appropriate system
* @param commandName
* @param commandExecutor
*/
public void registerCommand(String commandName, CommandTemplate commandExecutor) {
this.plugin.getCommand(commandName).setExecutor(new SpigotCommandHandler(commandExecutor));
}
}

View File

@ -0,0 +1,36 @@
package com.sekwah.advancedportals.coreconnector.spigot.container;
import com.sekwah.advancedportals.coreconnector.container.CommandSenderContainer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class SpigotCommandSenderContainer implements CommandSenderContainer {
private final CommandSender sender;
public SpigotCommandSenderContainer(CommandSender commandSender) {
this.sender = commandSender;
}
public void sendMessage(String message) {
sender.sendMessage(message);
}
public boolean isOp() {
return sender.isOp();
}
/**
* @return null if there isnt a player e.g. the console
*/
public SpigotPlayerContainer getPlayerContainer() {
if (sender instanceof Player) {
return new SpigotPlayerContainer((Player) sender);
}
return null;
}
public boolean hasPermission(String permission) {
return sender.hasPermission(permission);
}
}

View File

@ -0,0 +1,103 @@
package com.sekwah.advancedportals.coreconnector.spigot.container;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.entities.PlayerLocation;
import com.sekwah.advancedportals.core.entities.PortalLocation;
import com.sekwah.advancedportals.coreconnector.container.PlayerContainer;
import com.sekwah.advancedportals.coreconnector.container.WorldContainer;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.Wool;
import java.util.Arrays;
import java.util.UUID;
/**
* Just a temporary container for whenever advanced portals needs to get data from a player
*/
public class SpigotPlayerContainer implements PlayerContainer {
@Inject
private AdvancedPortalsCore portalsCore;
private final Player player;
public SpigotPlayerContainer(Player player) {
this.player = player;
}
public UUID getUUID() {
return player.getUniqueId();
}
public void sendMessage(String message) {
player.sendMessage(message);
}
public boolean isOp() {
return this.player.isOp();
}
public PlayerLocation getLoc() {
Location loc = this.player.getLocation();
return new PlayerLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ());
}
public double getEyeHeight() {
return 0;
}
public void teleport(PlayerLocation location) {
this.player.teleport(new Location(Bukkit.getWorld(location.worldName), location.posX, location.posY, location.posZ));
}
public boolean hasPermission(String permission) {
return this.player.hasPermission(permission);
}
public WorldContainer getWorld() {
return new SpigotWorldContainer(this.player.getWorld());
}
/**
* @param blockPos
* @param material
*/
public void sendFakeBlock(PortalLocation blockPos, String material) {
}
/**
* Only 1.12 and below supported
* @param blockPos
* @param material
* @param data
*/
public void sendFakeBlockWithData(PortalLocation blockPos, String material, byte data) {
}
public void giveWool(String dyeColor, String itemName, String... itemDescription) {
ItemStack regionselector = new Wool(DyeColor.valueOf(dyeColor)).toItemStack(1);
ItemMeta selectorname = regionselector.getItemMeta();
selectorname.setDisplayName(itemName);
selectorname.setLore(Arrays.asList(itemDescription));
regionselector.setItemMeta(selectorname);
this.player.getInventory().addItem(regionselector);
}
public void giveItem(String material, String itemName, String... itemDescription) {
ItemStack regionselector = new ItemStack(Material.getMaterial(material));
ItemMeta selectorname = regionselector.getItemMeta();
selectorname.setDisplayName(itemName);
selectorname.setLore(Arrays.asList(itemDescription));
regionselector.setItemMeta(selectorname);
this.player.getInventory().addItem(regionselector);
}
}

View File

@ -0,0 +1,40 @@
package com.sekwah.advancedportals.coreconnector.spigot.container;
import com.sekwah.advancedportals.core.entities.PortalLocation;
import com.sekwah.advancedportals.coreconnector.container.WorldContainer;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.material.Directional;
import org.bukkit.material.MaterialData;
public class SpigotWorldContainer implements WorldContainer {
private final World world;
public SpigotWorldContainer(World world) {
this.world = world;
}
public void setBlock(PortalLocation location, String material) {
this.world.getBlockAt(location.posX, location.posY, location.posZ).setType(Material.getMaterial(material));
}
public void setBlockData(PortalLocation location, byte data) {
MaterialData matData = world.getBlockAt(location.posX, location.posY, location.posZ).getState().getData();
if(matData instanceof Directional) {
System.out.println("IS DIRECTIONAL");
Directional dir = (Directional) world.getBlockAt(location.posX, location.posY, location.posZ).getState().getData();
dir.setFacingDirection(BlockFace.NORTH);
}
}
public String getBlock(PortalLocation location) {
return this.world.getBlockAt(location.posX, location.posY, location.posZ).getType().toString();
}
public byte getBlockData(PortalLocation location) {
return 0;
}
}

View File

@ -1,14 +1,12 @@
package com.sekwah.advancedportals.coreconnector;
package com.sekwah.advancedportals.coreconnector.spigot.info;
import com.sekwah.advancedportals.coreconnector.info.DataCollector;
import org.bukkit.Material;
/**
* Gets info from the specific implementation
*/
public class ConnectorDataCollector {
public class SpigotDataCollector implements DataCollector {
public boolean materialExists(String materialName) {
String sameCase = materialName.toUpperCase();
return Material.getMaterial(sameCase) != null;
}
}

View File

@ -35,7 +35,7 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
versionInts[i] = Integer.parseInt(versionNums[i]);
}
this.portalsCore = new AdvancedPortalsCore(this.getDataFolder(),
new SpigotInfoLogger(this), new CommandRegister(this), new ConnectorDataCollector(), versionInts);
new SpigotInfoLogger(this), new SpigotCommandRegister(this), new SpigotDataCollector(), versionInts);
}
else {
this.getLogger().warning("Could not parse mc version from: " + Bukkit.getVersion());

View File

@ -20,19 +20,19 @@ public class Listeners implements Listener {
@EventHandler
public void onJoinEvent(PlayerJoinEvent event) {
coreListeners.playerJoin(new PlayerContainer(event.getPlayer()));
coreListeners.playerJoin(new SpigotPlayerContainer(event.getPlayer()));
}
@EventHandler
public void onJoinEvent(PlayerQuitEvent event) {
coreListeners.playerLeave(new PlayerContainer(event.getPlayer()));
coreListeners.playerLeave(new SpigotPlayerContainer(event.getPlayer()));
}
@EventHandler(priority = EventPriority.HIGH)
public void onBlockPlace(BlockPlaceEvent event) {
if (!event.isCancelled()) {
Location blockloc = event.getBlock().getLocation();
this.coreListeners.blockPlace(new PlayerContainer(event.getPlayer()),
this.coreListeners.blockPlace(new SpigotPlayerContainer(event.getPlayer()),
new PortalLocation(blockloc.getWorld().getName(), blockloc.getBlockX(), blockloc.getBlockY(), blockloc.getBlockZ()), event.getBlockPlaced().getType().toString(),
event.getItemInHand().getType().toString(), event.getItemInHand().getItemMeta().getDisplayName());
}
@ -42,7 +42,7 @@ public class Listeners implements Listener {
public void onItemInteract(PlayerInteractEvent event) {
if (!event.isCancelled() && (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK) && event.getItem() != null) {
Location blockloc = event.getClickedBlock().getLocation();
boolean allowEvent = this.coreListeners.playerInteractWithBlock(new PlayerContainer(event.getPlayer()), event.getMaterial().toString(),
boolean allowEvent = this.coreListeners.playerInteractWithBlock(new SpigotPlayerContainer(event.getPlayer()), event.getMaterial().toString(),
event.getItem().getItemMeta().getDisplayName(),
new PortalLocation(blockloc.getWorld().getName(), blockloc.getBlockX(), blockloc.getBlockY(), blockloc.getBlockZ()),
event.getAction() == Action.LEFT_CLICK_BLOCK);