Skip to content

Commit

Permalink
more improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 4, 2024
1 parent ad63c61 commit 9c1c076
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.EntityUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.World;
Expand Down Expand Up @@ -34,8 +35,7 @@ public VehicleLimit() {
this.plugin = AnarchyExploitFixes.getInstance();
this.VEHICLE_TYPES = new HashSet<>(); // Used to cache entity types of vehicles for faster operations
// Prefill with known types;
this.VEHICLE_TYPES.add(EntityType.BOAT);
this.VEHICLE_TYPES.add(EntityType.CHEST_BOAT);
this.VEHICLE_TYPES.addAll(EntityUtil.BOATS);
this.VEHICLE_TYPES.addAll(EntityTags.HORSES.getValues());
this.VEHICLE_TYPES.addAll(EntityTags.MINECARTS.getValues());
Config config = AnarchyExploitFixes.getConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.utils.EntityUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -43,9 +43,10 @@ protected void register() {
@Override
public void onPacketReceiving(PacketEvent event) {
if (event.isPlayerTemporary()) return;
Player player = event.getPlayer();
Entity vehicle = player.getVehicle();
if (!(vehicle instanceof Boat)) return;
final Player player = event.getPlayer();
if (!player.isInsideVehicle()) return;
final Entity vehicle = player.getVehicle();
if (!EntityUtil.BOATS.contains(vehicle.getType())) return;

final UUID playerUniqueID = player.getUniqueId();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.moomoo.anarchyexploitfixes.utils;

import org.bukkit.entity.EntityType;

import java.util.HashSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class EntityUtil {

public static final HashSet<EntityType> BOATS = Stream.of(
EntityType.BOAT,
EntityType.CHEST_BOAT
).collect(Collectors.toCollection(HashSet::new));

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,29 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.EntityUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.vehicle.VehicleCreateEvent;

import java.util.Arrays;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class MinecartLimit implements AnarchyExploitFixesModule, Listener, Runnable {

private final AnarchyExploitFixes plugin;
private final HashSet<EntityType> MINECART_TYPES;
private final long checkPeriod;
private final int maxMinecartsPerChunk;
private final boolean logIsEnabled;

public MinecartLimit() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
this.MINECART_TYPES = Arrays.stream(EntityType.values())
.filter(type -> type.name().toUpperCase().contains("MINECART"))
.collect(Collectors.toCollection(HashSet::new));
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("chunk-limits.minecart-limit.enable",
"Limit the amount of minecarts to prevent lag.");
Expand Down Expand Up @@ -66,12 +59,12 @@ public boolean shouldEnable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onCreate(VehicleCreateEvent event) {
Vehicle vehicle = event.getVehicle();
if (!MINECART_TYPES.contains(vehicle.getType())) return;
if (!EntityUtil.MINECARTS.contains(vehicle.getType())) return;

int minecartCount = 0;

for (Entity entity : vehicle.getChunk().getEntities()) {
if (!MINECART_TYPES.contains(entity.getType())) continue;
if (!EntityUtil.MINECARTS.contains(entity.getType())) continue;
minecartCount++;
if (minecartCount <= maxMinecartsPerChunk) continue;

Expand All @@ -91,7 +84,7 @@ public void run() {
int minecartCount = 0;

for (Entity entity : chunk.getEntities()) {
if (!MINECART_TYPES.contains(entity.getType())) continue;
if (!EntityUtil.MINECARTS.contains(entity.getType())) continue;

minecartCount++;
if (minecartCount <= maxMinecartsPerChunk) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.utils.EntityUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -43,9 +43,10 @@ protected void register() {
@Override
public void onPacketReceiving(PacketEvent event) {
if (event.isPlayerTemporary()) return;
Player player = event.getPlayer();
Entity vehicle = player.getVehicle();
if (!(vehicle instanceof Boat)) return;
final Player player = event.getPlayer();
if (!player.isInsideVehicle()) return;
final Entity vehicle = player.getVehicle();
if (!EntityUtil.BOATS.contains(vehicle.getType())) return;

UUID playerUniqueID = player.getUniqueId();

Expand All @@ -64,6 +65,7 @@ public void onPacketReceiving(PacketEvent event) {
}

plugin.getServer().getScheduler().runTask(plugin, vehicle::remove);

if (logIsEnabled)
LogUtil.moduleLog(Level.INFO, log_name, "Prevented player '"+player.getName()+"' from boat flying");
if (shouldKickPlayer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ public void onPacketReceiving(PacketEvent event) {
event.setCancelled(true);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.moomoo.anarchyexploitfixes.utils;

import org.bukkit.entity.EntityType;

import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Collectors;

public class EntityUtil {

public static final HashSet<EntityType> BOATS = Arrays.stream(EntityType.values())
.filter(entityType -> entityType.name().toUpperCase().contains("BOAT"))
.collect(Collectors.toCollection(HashSet::new));

public static final HashSet<EntityType> MINECARTS = Arrays.stream(EntityType.values())
.filter(entityType -> entityType.name().toUpperCase().contains("MINECART"))
.collect(Collectors.toCollection(HashSet::new));

}

0 comments on commit 9c1c076

Please sign in to comment.