Skip to content

Commit

Permalink
check all faces instead of using Velocity#toLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 4, 2024
1 parent fc9bab4 commit 14c6cd7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.PlayerInventory;

import static me.moomoo.anarchyexploitfixes.utils.MaterialUtil.isSpawnEgg;

public class PreventUsingSpawnEggs implements AnarchyExploitFixesModule, Listener {

public PreventUsingSpawnEggs() {
Expand Down Expand Up @@ -57,7 +55,7 @@ private void onInteract(PlayerInteractEvent event) {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onDispense(BlockDispenseEvent event) {
if (isSpawnEgg(event.getItem())) {
if (MaterialUtil.isSpawnEgg(event.getItem())) {
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import org.bukkit.Location;
Expand All @@ -20,7 +21,6 @@
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;

import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -89,15 +89,19 @@ public void disable() {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockDispense(BlockDispenseEvent event) {
if (
MaterialUtil.isLiquidBucket(event.getItem().getType())
&& event.getVelocity().clone().toLocation(event.getBlock().getWorld()).getBlock().getType().equals(Material.END_PORTAL)
) {
if (MaterialUtil.isBlockDispenseBucket(event.getItem().getType()) && isNearEndPortal(event.getBlock())) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Prevented a dispenser from destroying an end portal!");
}
}

private boolean isNearEndPortal(Block dispenser) {
for (BlockFace face : BlockFace.values()) {
if (dispenser.getRelative(face).getType().equals(Material.END_PORTAL)) return true;
}
return false;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerBucketEvent(PlayerBucketEmptyEvent event) {
if (event.getBlockClicked().getRelative(event.getBlockFace()).getType().equals(Material.END_PORTAL)) {
Expand Down Expand Up @@ -161,7 +165,7 @@ private void onPistonExplode(EntityExplodeEvent event) {

private boolean isWithinEndProtectedRadius(Location location) {
if (!location.getWorld().getEnvironment().equals(World.Environment.THE_END)) return false;
return new Point(location.getBlockX(), location.getBlockZ()).distance(new Point(0,0)) <= endBedrockProtectRadius;
return LocationUtil.getFlatDistanceTo00(location) <= endBedrockProtectRadius;
}

private boolean isEndPortal(Material material) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

public class MaterialUtil {

public static boolean isLiquidBucket(ItemStack item) {
public static boolean isBlockDispenseBucket(ItemStack item) {
if (item == null) return false;
return isLiquidBucket(item.getType());
return isBlockDispenseBucket(item.getType());
}

public static boolean isLiquidBucket(Material material) {
public static boolean isBlockDispenseBucket(Material material) {
if (material == null) return false;
return switch (material) {
case WATER_BUCKET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import org.bukkit.Location;
Expand All @@ -19,7 +20,6 @@
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;

import java.awt.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.logging.Level;
Expand Down Expand Up @@ -86,15 +86,19 @@ public boolean shouldEnable() {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockDispense(BlockDispenseEvent event) {
if (
MaterialUtil.LIQUID_BUCKETS.contains(event.getItem().getType())
&& event.getVelocity().clone().toLocation(event.getBlock().getWorld()).getBlock().getType().equals(end_portal)
) {
if (MaterialUtil.BLOCK_DISPENSE_BUCKETS.contains(event.getItem().getType()) && isNearEndPortal(event.getBlock())) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Prevented a dispenser from destroying an end portal!");
}
}

private boolean isNearEndPortal(Block dispenser) {
for (BlockFace face : BlockFace.values()) {
if (dispenser.getRelative(face).getType().equals(end_portal)) return true;
}
return false;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerBucketEvent(PlayerBucketEmptyEvent event) {
if (event.getBlockClicked().getRelative(event.getBlockFace()).getType().equals(end_portal)) {
Expand Down Expand Up @@ -160,7 +164,7 @@ private void onPistonExplode(EntityExplodeEvent event) {

private boolean isWithinEndProtectedRadius(Location location) {
if (!location.getWorld().getEnvironment().equals(World.Environment.THE_END)) return false;
return new Point(location.getBlockX(), location.getBlockZ()).distance(new Point(0,0)) <= endBedrockProtectRadius;
return LocationUtil.getFlatDistanceTo00(location) <= endBedrockProtectRadius;
}

private boolean isEndPortal(Material material) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ public void onPacketReceiving(PacketEvent event) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static boolean isSpawnEgg(ItemStack item) {
.map(XMaterial::parseMaterial)
.collect(Collectors.toCollection(HashSet::new));

public static final HashSet<Material> LIQUID_BUCKETS = Stream.of(
public static final HashSet<Material> BLOCK_DISPENSE_BUCKETS = Stream.of(
XMaterial.WATER_BUCKET,
XMaterial.LAVA_BUCKET,
XMaterial.COD_BUCKET,
Expand Down

0 comments on commit 14c6cd7

Please sign in to comment.