Add PlayerListener.java to start protecting islands
This commit is contained in:
parent
8847430ed9
commit
9e4fc5fffd
|
|
@ -7,6 +7,7 @@ import com.alttd.cometskyblock.gui.GUIListener;
|
|||
import com.alttd.cometskyblock.listeners.BedListener;
|
||||
import com.alttd.cometskyblock.listeners.CobbestoneGeneratorListener;
|
||||
import com.alttd.cometskyblock.listeners.PlayerJoinListener;
|
||||
import com.alttd.cometskyblock.listeners.PlayerListener;
|
||||
import com.alttd.cometskyblock.managers.IslandManager;
|
||||
import com.alttd.cometskyblock.worldgenerator.MasterWorldGenerator;
|
||||
import lombok.Getter;
|
||||
|
|
@ -88,6 +89,7 @@ public class CometSkyBlockPlugin extends JavaPlugin implements CometSkyBlockAPI
|
|||
pm.registerEvents(new BedListener(this), this);
|
||||
pm.registerEvents(new CobbestoneGeneratorListener(this), this);
|
||||
pm.registerEvents(new GUIListener(this), this);
|
||||
pm.registerEvents(new PlayerListener(this), this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,161 @@
|
|||
package com.alttd.cometskyblock.listeners;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.block.EntityBlockFormEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.player.PlayerBucketEmptyEvent;
|
||||
import org.bukkit.event.player.PlayerBucketFillEvent;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
private final CometSkyBlockPlugin plugin;
|
||||
public PlayerListener(CometSkyBlockPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
// TODO -- extract all of the duplicate code
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onBlockIgnite(PlayerBucketEmptyEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerBucketFill(PlayerBucketFillEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onEntityBlockForm(EntityBlockFormEvent event) { // not a player listener
|
||||
World world = event.getEntity().getWorld();
|
||||
Island island = Island.getIsland(world.getUID());
|
||||
if (island == null) {
|
||||
return;
|
||||
}
|
||||
if (island.worldName() == null || island.worldName().isEmpty()) { // Worlds not generated by us will have this as null or empty
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Protect island visits from falling into the void
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void islandVisitProtection(EntityDamageEvent event) {
|
||||
Entity entity = event.getEntity();
|
||||
if (entity instanceof Player player) {
|
||||
Island island = Island.getIsland(player.getWorld().getUID());
|
||||
if (island == null) {
|
||||
return;
|
||||
}
|
||||
if (island.worldName() == null || island.worldName().isEmpty()) { // Worlds not generated by us will have this as null or empty
|
||||
return;
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
if (event.getCause() == EntityDamageEvent.DamageCause.VOID || player.getLocation().getY() < 0) {
|
||||
player.setFallDistance(0);
|
||||
player.teleportAsync(plugin.getServer().getWorlds().get(0).getSpawnLocation());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user