Rework listeners and add EntityListener.
This commit is contained in:
parent
88c3ee39ec
commit
b2eab51b72
|
|
@ -11,7 +11,8 @@ import com.alttd.cometskyblock.island.IslandData;
|
||||||
import com.alttd.cometskyblock.island.oregenerator.GeneratorHandler;
|
import com.alttd.cometskyblock.island.oregenerator.GeneratorHandler;
|
||||||
import com.alttd.cometskyblock.island.oregenerator.GeneratorLoader;
|
import com.alttd.cometskyblock.island.oregenerator.GeneratorLoader;
|
||||||
import com.alttd.cometskyblock.listeners.BedListener;
|
import com.alttd.cometskyblock.listeners.BedListener;
|
||||||
import com.alttd.cometskyblock.listeners.CobbestoneGeneratorListener;;
|
import com.alttd.cometskyblock.listeners.EntityListener;
|
||||||
|
import com.alttd.cometskyblock.listeners.cobblestoneGeneratorListener;;
|
||||||
import com.alttd.cometskyblock.listeners.PlayerListener;
|
import com.alttd.cometskyblock.listeners.PlayerListener;
|
||||||
import com.alttd.cometskyblock.managers.IslandManager;
|
import com.alttd.cometskyblock.managers.IslandManager;
|
||||||
import com.alttd.cometskyblock.worldgenerator.MasterWorldGenerator;
|
import com.alttd.cometskyblock.worldgenerator.MasterWorldGenerator;
|
||||||
|
|
@ -104,11 +105,16 @@ public class CometSkyBlockPlugin extends JavaPlugin implements CometSkyBlockAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadEventListeners() {
|
public void loadEventListeners() {
|
||||||
final PluginManager pm = getServer().getPluginManager();
|
// final PluginManager pm = getServer().getPluginManager();
|
||||||
pm.registerEvents(new BedListener(this), this);
|
// pm.registerEvents(new BedListener(this), this);
|
||||||
pm.registerEvents(new CobbestoneGeneratorListener(this), this);
|
// pm.registerEvents(new cobblestoneGeneratorListener(this), this);
|
||||||
pm.registerEvents(new GUIListener(this), this);
|
// pm.registerEvents(new GUIListener(this), this);
|
||||||
pm.registerEvents(new PlayerListener(this), this);
|
// pm.registerEvents(new PlayerListener(this), this);
|
||||||
|
new BedListener(this);
|
||||||
|
new cobblestoneGeneratorListener(this);
|
||||||
|
new GUIListener(this);
|
||||||
|
new PlayerListener(this);
|
||||||
|
new EntityListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.alttd.cometskyblock.gui;
|
package com.alttd.cometskyblock.gui;
|
||||||
|
|
||||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||||
|
import com.alttd.cometskyblock.listeners.EventListener;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
@ -12,12 +13,13 @@ import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||||
/**
|
/**
|
||||||
* Basically a handler to redirect events to our GUI implementations
|
* Basically a handler to redirect events to our GUI implementations
|
||||||
*/
|
*/
|
||||||
public class GUIListener implements Listener {
|
public class GUIListener extends EventListener {
|
||||||
|
|
||||||
protected final CometSkyBlockPlugin plugin;
|
protected final CometSkyBlockPlugin plugin;
|
||||||
|
|
||||||
public GUIListener(CometSkyBlockPlugin plugin) {
|
public GUIListener(CometSkyBlockPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.register(this.plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,12 @@ import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
* By default Gamerule DO_INSOMNIA is disabled, the moment an island member sleeps this gamerule will be enabled.
|
* By default Gamerule DO_INSOMNIA is disabled, the moment an island member sleeps this gamerule will be enabled.
|
||||||
* Still debating if this should only be enabled for those that have slept
|
* Still debating if this should only be enabled for those that have slept
|
||||||
*/
|
*/
|
||||||
public class BedListener implements Listener {
|
public class BedListener extends EventListener {
|
||||||
|
|
||||||
private final CometSkyBlockPlugin plugin;
|
private final CometSkyBlockPlugin plugin;
|
||||||
public BedListener(CometSkyBlockPlugin plugin) {
|
public BedListener(CometSkyBlockPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.register(this.plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
|
|
@ -28,23 +29,10 @@ public class BedListener implements Listener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
if(!canInteract(event, player))
|
||||||
|
return;
|
||||||
|
|
||||||
World world = player.getWorld();
|
World world = player.getWorld();
|
||||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
|
||||||
if (!world.getUID().equals(islandPlayer.islandUUID())) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Island island = Island.getIsland(world.getUID());
|
|
||||||
if (island == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!island.canBuild(player.getUniqueId())) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
world.setGameRule(GameRule.DO_INSOMNIA, true);
|
world.setGameRule(GameRule.DO_INSOMNIA, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.alttd.cometskyblock.listeners;
|
||||||
|
|
||||||
|
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
|
||||||
|
|
||||||
|
public class EntityListener extends EventListener {
|
||||||
|
|
||||||
|
private final CometSkyBlockPlugin plugin;
|
||||||
|
public EntityListener(CometSkyBlockPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.register(this.plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true)
|
||||||
|
public void onEntityTargetPlayer(EntityTargetLivingEntityEvent event) {
|
||||||
|
if (!(event.getTarget() instanceof Player player))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(!canInteract(event, player))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.alttd.cometskyblock.listeners;
|
||||||
|
|
||||||
|
import com.alttd.cometskyblock.island.Island;
|
||||||
|
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public class EventListener implements Listener {
|
||||||
|
protected boolean isRegistered = false;
|
||||||
|
|
||||||
|
public void register(JavaPlugin instance) {
|
||||||
|
Bukkit.getServer().getPluginManager().registerEvents(this, instance);
|
||||||
|
isRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregister() {
|
||||||
|
HandlerList.unregisterAll(this);
|
||||||
|
isRegistered = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean canInteract(Cancellable event, Player player) {
|
||||||
|
World world = player.getWorld();
|
||||||
|
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
||||||
|
if (world.getUID().equals(Bukkit.getWorlds().get(0).getUID()))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// if (!world.getUID().equals(islandPlayer.islandUUID())) {
|
||||||
|
// return cancelEvent(event);
|
||||||
|
// }
|
||||||
|
|
||||||
|
Island island = Island.getIsland(world.getUID());
|
||||||
|
if (island == null) {
|
||||||
|
return cancelEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Worlds not generated by CometSkyblock will have this as null or empty
|
||||||
|
// We do not care about these worlds - yet
|
||||||
|
if (island.worldName() == null || island.worldName().isEmpty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!island.canInteract(player.getUniqueId())) {
|
||||||
|
return cancelEvent(event);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean cancelEvent(Cancellable event) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
package com.alttd.cometskyblock.listeners;
|
|
||||||
|
|
||||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
|
||||||
import com.alttd.cometskyblock.events.IslandPlayerJoinEvent;
|
|
||||||
import com.alttd.cometskyblock.island.Island;
|
|
||||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
|
||||||
|
|
||||||
public class PlayerJoinListener implements Listener {
|
|
||||||
|
|
||||||
private final CometSkyBlockPlugin plugin;
|
|
||||||
public PlayerJoinListener(CometSkyBlockPlugin plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
|
||||||
public void onPlayerJoinEvent(PlayerJoinEvent event) {
|
|
||||||
Player player = event.getPlayer();
|
|
||||||
|
|
||||||
if (!player.hasPlayedBefore())
|
|
||||||
return;
|
|
||||||
|
|
||||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
|
||||||
if (islandPlayer.islandId() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Bukkit.getServer().getPluginManager().callEvent(new IslandPlayerJoinEvent(islandPlayer.toRecord()));
|
|
||||||
if (!player.getWorld().getUID().equals(islandPlayer.islandUUID()))
|
|
||||||
return;
|
|
||||||
|
|
||||||
Island island = Island.getIsland(islandPlayer.islandUUID());
|
|
||||||
if (island == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
island.teleport(player, player.getLocation());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
package com.alttd.cometskyblock.listeners;
|
|
||||||
|
|
||||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
|
||||||
import com.alttd.cometskyblock.events.IslandPlayerJoinEvent;
|
|
||||||
import com.alttd.cometskyblock.events.IslandPlayerLeaveEvent;
|
|
||||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
|
||||||
|
|
||||||
public class PlayerLeaveListener implements Listener {
|
|
||||||
|
|
||||||
private final CometSkyBlockPlugin plugin;
|
|
||||||
public PlayerLeaveListener(CometSkyBlockPlugin plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
|
||||||
public void onPlayerJoinEvent(PlayerJoinEvent event) {
|
|
||||||
Player player = event.getPlayer();
|
|
||||||
|
|
||||||
if (!player.hasPlayedBefore())
|
|
||||||
return;
|
|
||||||
|
|
||||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
|
||||||
if (islandPlayer.islandId() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Bukkit.getServer().getPluginManager().callEvent(new IslandPlayerLeaveEvent(islandPlayer.toRecord()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
package com.alttd.cometskyblock.listeners;
|
package com.alttd.cometskyblock.listeners;
|
||||||
|
|
||||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||||
|
import com.alttd.cometskyblock.events.IslandPlayerJoinEvent;
|
||||||
|
import com.alttd.cometskyblock.events.IslandPlayerLeaveEvent;
|
||||||
import com.alttd.cometskyblock.island.Island;
|
import com.alttd.cometskyblock.island.Island;
|
||||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.entity.Entity;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.Cancellable;
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
|
@ -16,17 +17,15 @@ import org.bukkit.event.block.Action;
|
||||||
import org.bukkit.event.block.BlockBreakEvent;
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
import org.bukkit.event.block.BlockPlaceEvent;
|
import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
import org.bukkit.event.block.EntityBlockFormEvent;
|
import org.bukkit.event.block.EntityBlockFormEvent;
|
||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
import org.bukkit.event.player.*;
|
||||||
import org.bukkit.event.player.PlayerBucketEmptyEvent;
|
|
||||||
import org.bukkit.event.player.PlayerBucketFillEvent;
|
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
public class PlayerListener implements Listener {
|
public class PlayerListener extends EventListener {
|
||||||
|
|
||||||
private final CometSkyBlockPlugin plugin;
|
private final CometSkyBlockPlugin plugin;
|
||||||
public PlayerListener(CometSkyBlockPlugin plugin) {
|
public PlayerListener(CometSkyBlockPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.register(this.plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
|
|
@ -95,36 +94,39 @@ public class PlayerListener implements Listener {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
boolean canInteract(Cancellable event, Player player) {
|
@EventHandler(ignoreCancelled = true)
|
||||||
World world = player.getWorld();
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
|
if (!player.hasPlayedBefore())
|
||||||
|
return;
|
||||||
|
|
||||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
||||||
if (world.getUID().equals(Bukkit.getWorlds().get(0).getUID()))
|
if (islandPlayer.islandId() == 0) {
|
||||||
return false;
|
return;
|
||||||
|
|
||||||
if (!world.getUID().equals(islandPlayer.islandUUID())) {
|
|
||||||
return cancelEvent(event);
|
|
||||||
}
|
}
|
||||||
|
Bukkit.getServer().getPluginManager().callEvent(new IslandPlayerJoinEvent(islandPlayer.toRecord()));
|
||||||
|
if (!player.getWorld().getUID().equals(islandPlayer.islandUUID()))
|
||||||
|
return;
|
||||||
|
|
||||||
Island island = Island.getIsland(world.getUID());
|
Island island = Island.getIsland(islandPlayer.islandUUID());
|
||||||
if (island == null) {
|
if (island == null) {
|
||||||
return cancelEvent(event);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Worlds not generated by CometSkyblock will have this as null or empty
|
island.teleport(player, player.getLocation());
|
||||||
// We do not care about these worlds - yet
|
|
||||||
if (island.worldName() == null || island.worldName().isEmpty()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!island.canBuild(player.getUniqueId())) {
|
|
||||||
return cancelEvent(event);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean cancelEvent(Cancellable event) {
|
@EventHandler(ignoreCancelled = true)
|
||||||
event.setCancelled(true);
|
public void onPlayerLeave(PlayerQuitEvent event) {
|
||||||
return false;
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
|
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(player.getUniqueId());
|
||||||
|
if (islandPlayer.islandId() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Bukkit.getServer().getPluginManager().callEvent(new IslandPlayerLeaveEvent(islandPlayer.toRecord()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,23 +6,22 @@ import org.bukkit.Material;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockState;
|
import org.bukkit.block.BlockState;
|
||||||
import org.bukkit.entity.Item;
|
import org.bukkit.entity.Item;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.block.*;
|
import org.bukkit.event.block.*;
|
||||||
import org.bukkit.metadata.FixedMetadataValue;
|
import org.bukkit.metadata.FixedMetadataValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CobbestoneGeneratorListener implements Listener {
|
public class cobblestoneGeneratorListener extends EventListener {
|
||||||
|
|
||||||
public static final @NotNull String GENERATOR_METADATA_KEY = "comet:oregenerator";
|
public static final @NotNull String GENERATOR_METADATA_KEY = "comet:oregenerator";
|
||||||
|
|
||||||
private final CometSkyBlockPlugin plugin;
|
private final CometSkyBlockPlugin plugin;
|
||||||
|
|
||||||
public CobbestoneGeneratorListener(CometSkyBlockPlugin plugin) {
|
public cobblestoneGeneratorListener(CometSkyBlockPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.register(this.plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
Loading…
Reference in New Issue
Block a user