Skip to content

Commit

Permalink
Improve resiliency of the correlation listener
Browse files Browse the repository at this point in the history
Signed-off-by: TTtie <[email protected]>
  • Loading branch information
TTtie committed Feb 20, 2024
1 parent 7c444ae commit f83a191
Showing 1 changed file with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package tc.oc.pgm.util.listener;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.entity.minecart.ExplosiveMinecart;
import org.bukkit.event.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent;
Expand All @@ -22,6 +25,7 @@
public class TNTMinecartPlacementListener implements Listener {
private Player lastPlacer;
private ItemStack placingStack;
private Location railLocation;

private static void handleCall(Event pgmEvent, Event bukkitEvent) {
if (bukkitEvent instanceof Cancellable) {
Expand All @@ -39,34 +43,51 @@ public void onInteraction(PlayerInteractEvent event) {
if (stack != null && stack.getType() == Material.EXPLOSIVE_MINECART) {
lastPlacer = event.getPlayer();
placingStack = stack.clone();
railLocation = event.getClickedBlock().getLocation();
}
}

@EventHandler(ignoreCancelled = true)
@EventHandler
public void onVehicleCreate(VehicleCreateEvent event) {
if (lastPlacer != null) {
PlayerSpawnEntityEvent pgmEvent =
new PlayerSpawnEntityEvent(
lastPlacer,
event.getVehicle(),
event.getVehicle().getLocation(),
lastPlacer.getItemInHand());
handleCall(pgmEvent, event);
/*
* Starting from 1.11.2, VehicleCreateEvent is natively cancellable.
* Even then, we should account for this case and reset the variables
* to ensure the tracking works reliably.
*/
if (!(event instanceof Cancellable) || !((Cancellable) event).isCancelled()) {
if (lastPlacer != null) {
Vehicle vehicle = event.getVehicle();
Location vehicleLocation = vehicle.getLocation();

/*
* This block only exists to ensure consistency, as VehicleCreateEvent
* is natively cancellable starting from 1.11.2. The TNT minecart is
* still being used up, however.
*/
if (pgmEvent.isCancelled()) {
if (!(event instanceof Cancellable)) {
event.getVehicle().remove();
if (vehicle instanceof ExplosiveMinecart
&& areBlockLocationsEqual(railLocation, vehicleLocation)) {
ItemStack itemInHand = lastPlacer.getItemInHand();

PlayerSpawnEntityEvent pgmEvent =
new PlayerSpawnEntityEvent(
lastPlacer,
vehicle,
vehicleLocation,
itemInHand != null ? itemInHand : placingStack);
handleCall(pgmEvent, event);

if (pgmEvent.isCancelled()) {
if (!(event instanceof Cancellable)) {
vehicle.remove();
}
lastPlacer.getInventory().setItemInHand(placingStack);
}
}
lastPlacer.getInventory().setItemInHand(placingStack);
}
}

lastPlacer = null;
placingStack = null;
railLocation = null;
}

private static boolean areBlockLocationsEqual(Location locA, Location locB) {
return locA.getBlockX() == locB.getBlockX()
&& locA.getBlockY() == locB.getBlockY()
&& locA.getBlockZ() == locB.getBlockZ();
}
}

0 comments on commit f83a191

Please sign in to comment.