feat: make services available as singletons
This commit is contained in:
parent
43c8823e4a
commit
c446c1602a
|
@ -19,6 +19,8 @@ public class AdvancedPortalsCore {
|
|||
|
||||
public static final String version = "1.0.0";
|
||||
|
||||
public static final String lastTranslationUpdate = "1.0.0";
|
||||
|
||||
private final InfoLogger infoLogger;
|
||||
private final DataStorage dataStorage;
|
||||
|
||||
|
|
|
@ -0,0 +1,144 @@
|
|||
package com.sekwah.advancedportals.core;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
|
||||
import com.sekwah.advancedportals.core.connector.containers.WorldContainer;
|
||||
import com.sekwah.advancedportals.core.data.BlockLocation;
|
||||
import com.sekwah.advancedportals.core.data.PlayerLocation;
|
||||
import com.sekwah.advancedportals.core.repository.ConfigRepository;
|
||||
import com.sekwah.advancedportals.core.services.PortalServices;
|
||||
import com.sekwah.advancedportals.core.services.PortalTempDataServices;
|
||||
import com.sekwah.advancedportals.core.util.Lang;
|
||||
|
||||
public class CoreListeners {
|
||||
|
||||
@Inject
|
||||
private PortalTempDataServices portalTempDataServices;
|
||||
|
||||
@Inject
|
||||
private PortalServices portalServices;
|
||||
|
||||
@Inject
|
||||
private ConfigRepository configRepository;
|
||||
|
||||
public void playerJoin(PlayerContainer player) {
|
||||
this.portalTempDataServices.activateCooldown(player);
|
||||
if(player.isOp()) {
|
||||
if(!Lang.translate("translatedata.lastchange").equals(AdvancedPortalsCore.lastTranslationUpdate)) {
|
||||
player.sendMessage(Lang.translate("messageprefix.negative")
|
||||
+ Lang.translateInsertVariables("translatedata.translationsoutdated", configRepository.getTranslation()));
|
||||
player.sendMessage(Lang.translate("messageprefix.negative")
|
||||
+ Lang.translate("translatedata.replacecommand"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void teleportEvent(PlayerContainer player) {
|
||||
this.portalTempDataServices.activateCooldown(player);
|
||||
}
|
||||
|
||||
public void playerLeave(PlayerContainer player) {
|
||||
this.portalTempDataServices.playerLeave(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param loc where the entity spawns
|
||||
* @return if the entity is allowed to spawn
|
||||
*/
|
||||
public boolean mobSpawn(PlayerLocation loc) {
|
||||
return !this.portalServices.inPortalRegion(loc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player
|
||||
* @param fromLoc
|
||||
* @param toLoc
|
||||
* @return if the player is allowed to move
|
||||
*/
|
||||
public boolean playerMove(PlayerContainer player, PlayerLocation fromLoc, PlayerLocation toLoc) {
|
||||
return this.portalServices.playerMove(player, fromLoc, toLoc);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param fromPos
|
||||
* @param toPos
|
||||
* @return if movement is allowed
|
||||
*/
|
||||
public boolean liquidFlow(BlockLocation fromPos, BlockLocation toPos) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @player player causing the event (or null if not a player)
|
||||
* @param blockPos
|
||||
* @param blockMaterial
|
||||
* @return if the block is allowed to break
|
||||
*/
|
||||
public boolean blockBreak(PlayerContainer player, BlockLocation blockPos, String blockMaterial) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @player player causing the event (or null if not a player)
|
||||
* @param blockPos
|
||||
* @param blockMaterial
|
||||
* @return if the block is allowed to be placed
|
||||
*/
|
||||
public boolean blockPlace(PlayerContainer player, BlockLocation blockPos, String blockMaterial, String itemInHandMaterial, String itemInHandName) {
|
||||
if(itemInHandName != null && player != null && player.hasPermission("advancedportals.build")) {
|
||||
WorldContainer world = player.getWorld();
|
||||
if(itemInHandName.equals("\u00A75Portal Block Placer")) {
|
||||
world.setBlock(blockPos, "PORTAL");
|
||||
return false;
|
||||
}
|
||||
else if(itemInHandName.equals("\u00A78End Portal Block Placer")) {
|
||||
world.setBlock(blockPos, "ENDER_PORTAL");
|
||||
return false;
|
||||
}
|
||||
else if(itemInHandName.equals("\u00A78Gateway Block Placer")) {
|
||||
world.setBlock(blockPos, "END_GATEWAY");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the block is allowed to be interacted with e.g. a lever
|
||||
* @player player causing the event (or null if not a player)
|
||||
* @param blockPos
|
||||
* @return
|
||||
*/
|
||||
public boolean blockInteract(PlayerContainer player, BlockLocation blockPos) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player
|
||||
* @param blockLoc
|
||||
* @param leftClick true = left click, false = right click
|
||||
* @return if player is allowed to interact with block
|
||||
*/
|
||||
public boolean playerInteractWithBlock(PlayerContainer player, String materialName, String itemName,
|
||||
BlockLocation blockLoc, boolean leftClick) {
|
||||
if(itemName != null && (player.isOp() || player.hasPermission("advancedportals.createportal")) &&
|
||||
materialName.equalsIgnoreCase(this.configRepository.getSelectorMaterial())
|
||||
&& (!this.configRepository.getUseOnlySpecialAxe() || itemName.equals("\u00A7ePortal Region Selector"))) {
|
||||
this.portalTempDataServices.playerSelectorActivate(player, blockLoc, leftClick);
|
||||
return false;
|
||||
}
|
||||
else if(itemName != null && leftClick && itemName.equals("\u00A75Portal Block Placer") && player.hasPermission("advancedportals.build")) {
|
||||
WorldContainer world = player.getWorld();
|
||||
if(world.getBlockData(blockLoc) == 1) {
|
||||
world.setBlockData(blockLoc, (byte) 2);
|
||||
}
|
||||
else {
|
||||
world.setBlockData(blockLoc, (byte) 1);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -9,11 +9,22 @@ package com.sekwah.advancedportals.core.data;
|
|||
*/
|
||||
public class PlayerTempData {
|
||||
|
||||
/**
|
||||
* Portal selection position 1
|
||||
*/
|
||||
private BlockLocation pos1;
|
||||
|
||||
/**
|
||||
* Portal selection position 2
|
||||
*/
|
||||
private BlockLocation pos2;
|
||||
|
||||
private long lastAttempt;
|
||||
/**
|
||||
* Used for things like join cooldowns
|
||||
* TODO either store a hashmap of cool-downs on a portal, or a hashmap of cool-downs for portals on a player
|
||||
* Can be switched at a later date
|
||||
*/
|
||||
private long globalCooldown;
|
||||
|
||||
private String selectedPortal;
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.sekwah.advancedportals.core.config.ConfigProvider;
|
|||
import com.sekwah.advancedportals.core.data.DataStorage;
|
||||
import com.sekwah.advancedportals.core.repository.ConfigRepository;
|
||||
import com.sekwah.advancedportals.core.repository.impl.ConfigRepositoryImpl;
|
||||
import com.sekwah.advancedportals.core.services.PortalServices;
|
||||
import com.sekwah.advancedportals.core.util.InfoLogger;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.sekwah.advancedportals.core.repository;
|
||||
|
||||
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
|
||||
import com.sekwah.advancedportals.core.data.BlockLocation;
|
||||
import com.sekwah.advancedportals.core.data.PlayerLocation;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface IPortalTempDataRepository {
|
||||
void addSelectedPortal(UUID selectedPlayer, String portal);
|
||||
|
||||
void removeSelectedPortal(UUID uuid);
|
||||
|
||||
void addSelectedPosition(UUID uuid, boolean isPos1, BlockLocation portalLocation);
|
||||
|
||||
void removeSelectedPosition(UUID uuid, boolean isPos1);
|
||||
|
||||
void removeAllSelectedHand(UUID uuid);
|
||||
|
||||
void activateCooldown(PlayerContainer player);
|
||||
|
||||
void playerLeave(PlayerContainer player);
|
||||
|
||||
boolean inPortalRegion(PlayerLocation loc);
|
||||
}
|
|
@ -11,6 +11,7 @@ import com.sekwah.advancedportals.core.destination.Destination;
|
|||
import com.sekwah.advancedportals.core.repository.IDestinationRepository;
|
||||
import com.sekwah.advancedportals.core.util.Lang;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
|
@ -20,6 +21,7 @@ import java.util.HashMap;
|
|||
* Handles logic for all destination, this is a transient layer so it should
|
||||
* not store any information.
|
||||
*/
|
||||
@Singleton
|
||||
public class DestinationServices {
|
||||
|
||||
|
||||
|
|
|
@ -1,52 +1,42 @@
|
|||
package com.sekwah.advancedportals.core.services;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
|
||||
import com.sekwah.advancedportals.core.data.BlockLocation;
|
||||
import com.sekwah.advancedportals.core.data.DataTag;
|
||||
import com.sekwah.advancedportals.core.data.PlayerLocation;
|
||||
import com.sekwah.advancedportals.core.repository.IPortalRepository;
|
||||
import com.sekwah.advancedportals.core.portal.AdvancedPortal;
|
||||
|
||||
import java.util.UUID;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
@Singleton
|
||||
public class PortalServices {
|
||||
|
||||
private final IPortalRepository portalRepository;
|
||||
|
||||
@Inject
|
||||
public PortalServices(IPortalRepository portalRepository) {
|
||||
this.portalRepository = portalRepository;
|
||||
}
|
||||
|
||||
public void addSelectedPortal(UUID selectedPlayer, String portal) {
|
||||
//portalRepository.save(selectedPlayer, portal);
|
||||
}
|
||||
|
||||
public void removeSelectedPortal(UUID uuid) {
|
||||
//selectedPortal.invalidate(uuid);
|
||||
}
|
||||
|
||||
public void addSelectedPosition(UUID uuid, boolean isPos1, BlockLocation portalLocation) {
|
||||
//selectedPositions.put(uuid, isPos1, portalLocation);
|
||||
}
|
||||
|
||||
public void removeSelectedPosition(UUID uuid, boolean isPos1) {
|
||||
//selectedPositions.remove(uuid, isPos1);
|
||||
}
|
||||
|
||||
public void removeAllSelectedHand(UUID uuid) {
|
||||
//selectedPositions.remove(uuid, true);
|
||||
//selectedPositions.remove(uuid, false);
|
||||
}
|
||||
|
||||
public void activateCooldown(PlayerContainer player) {
|
||||
|
||||
}
|
||||
|
||||
public void playerLeave(PlayerContainer player) {
|
||||
public void loadPortals() {
|
||||
|
||||
}
|
||||
|
||||
public boolean inPortalRegion(PlayerLocation loc) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean playerMove(PlayerContainer player, PlayerLocation fromLoc, PlayerLocation toLoc) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public ImmutableList<? extends Map.Entry<String, AdvancedPortal>> getPortals() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean removePortal(String name, PlayerContainer player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public AdvancedPortal createPortal(String name, PlayerContainer player, ArrayList<DataTag> portalTags) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean removePlayerSelection(PlayerContainer player) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.sekwah.advancedportals.core.services;
|
||||
|
||||
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
|
||||
import com.sekwah.advancedportals.core.data.BlockLocation;
|
||||
import com.sekwah.advancedportals.core.data.PlayerTempData;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Singleton
|
||||
public final class PortalTempDataServices {
|
||||
|
||||
/**
|
||||
* Possibly change to the cache map Aztec was talking about
|
||||
*/
|
||||
private Map<UUID, PlayerTempData> tempDataMap = new HashMap<>();
|
||||
|
||||
public void activateCooldown(PlayerContainer player) {
|
||||
}
|
||||
|
||||
public void playerLeave(PlayerContainer player) {
|
||||
}
|
||||
|
||||
public void playerSelectorActivate(PlayerContainer player, BlockLocation blockLoc, boolean leftClick) {
|
||||
}
|
||||
}
|
|
@ -24,6 +24,11 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
|
|||
|
||||
injector.injectMembers(this.portalsCore);
|
||||
|
||||
Listeners listeners = injector.getInstance(Listeners.class);
|
||||
injector.injectMembers(listeners);
|
||||
this.getServer().getPluginManager().registerEvents(listeners, this);
|
||||
|
||||
// Try to do this after setting up everything that would need to be injected to.
|
||||
this.portalsCore.onEnable();
|
||||
|
||||
new Metrics(this);
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.sekwah.advancedportals.spigot;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.sekwah.advancedportals.core.CoreListeners;
|
||||
import com.sekwah.advancedportals.core.data.BlockLocation;
|
||||
import com.sekwah.advancedportals.spigot.connector.container.SpigotPlayerContainer;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class Listeners implements Listener {
|
||||
|
||||
@Inject
|
||||
private CoreListeners coreListeners;
|
||||
|
||||
@EventHandler
|
||||
public void onJoinEvent(PlayerJoinEvent event) {
|
||||
coreListeners.playerJoin(new SpigotPlayerContainer(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuitEvent(PlayerQuitEvent event) {
|
||||
// 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 SpigotPlayerContainer(event.getPlayer()),
|
||||
new BlockLocation(blockloc.getWorld().getName(), blockloc.getBlockX(), blockloc.getBlockY(), blockloc.getBlockZ()), event.getBlockPlaced().getType().toString(),
|
||||
event.getItemInHand().getType().toString(), event.getItemInHand().getItemMeta().getDisplayName());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
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 SpigotPlayerContainer(event.getPlayer()), event.getMaterial().toString(),
|
||||
event.getItem().getItemMeta().getDisplayName(),
|
||||
new BlockLocation(blockloc.getWorld().getName(), blockloc.getBlockX(), blockloc.getBlockY(), blockloc.getBlockZ()),
|
||||
event.getAction() == Action.LEFT_CLICK_BLOCK);
|
||||
event.setCancelled(!allowEvent);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue