Fixes enderpearl usage and flags

https://github.com/BentoBoxWorld/Boxed/issues/50
This commit is contained in:
tastybento 2023-05-28 15:08:30 -07:00
parent 9d9b4ca3fb
commit 48284c92d7
2 changed files with 88 additions and 10 deletions

View File

@ -2,8 +2,10 @@ package world.bentobox.boxed.listeners;
import java.io.IOException;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EnderPearl;
import org.bukkit.entity.EntityType;
@ -12,8 +14,11 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.boxed.Boxed;
/**
@ -32,34 +37,100 @@ public class EnderPearlListener implements Listener {
this.addon = addon;
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent e) {
if (!addon.inWorld(e.getFrom()) || !e.getPlayer().getGameMode().equals(GameMode.SURVIVAL)) {
return;
}
User u = User.getInstance(e.getPlayer());
// If the to is outside the box, cancel it
if (e.getTo() != null) {
Island i = addon.getIslands().getIsland(e.getFrom().getWorld(), u);
if (i == null || !i.onIsland(e.getTo())) {
u.sendMessage("boxed.general.errors.no-teleport-outside");
e.setCancelled(true);
return;
}
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onEnderPearlLand(ProjectileHitEvent e) {
if (!e.getEntityType().equals(EntityType.ENDER_PEARL)
|| e.getHitBlock() == null
|| !addon.inWorld(e.getHitBlock().getLocation())) {
|| !addon.inWorld(e.getHitBlock().getLocation())
|| !Boxed.ALLOW_MOVE_BOX.isSetForWorld(e.getHitBlock().getWorld())
) {
return;
}
// Moving box is allowed
Location l = e.getHitBlock().getRelative(BlockFace.UP).getLocation();
World w = e.getHitBlock().getWorld();
EnderPearl ep = (EnderPearl)e.getEntity();
if (ep.getShooter() instanceof Player player) {
User u = User.getInstance(player);
// Check if enderpearl is inside or outside the box
// Get user's box
Island is = addon.getIslands().getIsland(w, u);
if (is == null) {
return; // Nothing to do
}
// Get the box that the player is in
addon.getIslands().getIslandAt(u.getLocation()).ifPresent(fromIsland -> {
// Check that it is their box
if (!is.getUniqueId().equals(fromIsland.getUniqueId())) {
return;
}
// Find where the pearl landed
addon.getIslands().getIslandAt(l).ifPresentOrElse(toIsland -> {
if (fromIsland.getUniqueId().equals(toIsland.getUniqueId())) {
if (!toIsland.onIsland(l)) {
// Moving is allowed
moveBox(u, fromIsland, l);
Util.teleportAsync(player, l);
return;
}
} else {
// Different island. This is never allowed. Cancel the throw
e.setCancelled(true);
u.sendMessage("boxed.general.errors.no-teleport-outside");
return;
}
}, () -> {
// No island. This is never allowed. Cancel the throw
e.setCancelled(true);
u.sendMessage("boxed.general.errors.no-teleport-outside");
return;
});
});
addon.getIslands().getIslandAt(l).ifPresent(i -> {
// Check flag
if (i.isAllowed(u, Boxed.MOVE_BOX) && addon.getIslands().isSafeLocation(l)) {
// Reset home locations
i.getMemberSet().forEach(uuid -> addon.getIslands().setHomeLocation(uuid, l));
try {
i.setProtectionCenter(l);
i.setSpawnPoint(l.getWorld().getEnvironment(), l);
u.getPlayer().playSound(l, Sound.ENTITY_GENERIC_EXPLODE, 2F, 2F);
} catch (IOException e1) {
addon.logError("Could not move box " + e1.getMessage());
}
}
});
}
}
private void moveBox(User u, Island fromIsland, Location l) {
// Reset home locations
fromIsland.getMemberSet().forEach(uuid -> addon.getIslands().setHomeLocation(uuid, l));
try {
fromIsland.setProtectionCenter(l);
fromIsland.setSpawnPoint(l.getWorld().getEnvironment(), l);
u.getPlayer().playSound(l, Sound.ENTITY_GENERIC_EXPLODE, 2F, 2F);
} catch (IOException e1) {
addon.logError("Could not move box " + e1.getMessage());
}
}
}

View File

@ -15,6 +15,7 @@ boxed:
already-have-island: '&c You already have a box!'
no-safe-location: '&c No safe location found in box!'
not-owner: '&c You are not the owner of your team!'
no-teleport-outside: "&c You cannot teleport outside of your box"
commands:
boxed:
help:
@ -305,6 +306,12 @@ boxed:
deleted-island: '&a Area at &e [xyz] &a has been successfully regenerated.'
protection:
flags:
ALLOW_MOVE_BOX:
name: Box moving
description: |-
&a Allow players to move
&a their box by throwing
&a enderpearls
ELYTRA:
description: Toggle use
ENDERMAN_GRIEFING: