Skip to content

Commit

Permalink
cleanup events
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 12, 2024
1 parent f761116 commit f67af6d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,22 @@
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Snowball;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PostSnowballExplodeEvent extends Event {
public class PostSnowballExplodeEvent extends SnowballExplodeEvent {

private static final @NotNull HandlerList handlers = new HandlerList();

private final @NotNull Snowball snowball;
private final @Nullable Entity hitEntity;
private final @NotNull Location explodeLocation;
private final float location;
private final boolean setFire, breakBlocks, hasExploded;
private final boolean hasExploded;

public PostSnowballExplodeEvent(
@NotNull Snowball snowball, @Nullable Entity hitEntity, @NotNull Location location,
float power, boolean setFire, boolean breakBlocks, boolean hasExploded , boolean isAsync
) {
super(isAsync);
this.snowball = snowball;
this.hitEntity = hitEntity;
this.explodeLocation = location;
this.location = power;
this.setFire = setFire;
this.breakBlocks = breakBlocks;
public PostSnowballExplodeEvent(@NotNull Snowball snowball, @Nullable Entity hitEntity,
@NotNull Location location, float power, boolean fire, boolean brokeBlocks, boolean hasExploded) {
super(snowball, hitEntity, location, power, fire, brokeBlocks);
this.hasExploded = hasExploded;
}

public @NotNull Snowball getSnowball() {
return snowball;
}
public @Nullable Entity getHitEntity() {
return hitEntity;
}
public @NotNull Location getExplodeLocation() {
return explodeLocation;
}
public float getExplosionPower() {
return location;
}
public boolean hasSetFire() {
return setFire;
}
public boolean hasBrokenBlocks() {
return breakBlocks;
}
public boolean hasExploded() {
return hasExploded;
}
Expand All @@ -59,6 +28,7 @@ public boolean hasExploded() {
public HandlerList getHandlers() {
return handlers;
}

@NotNull
public static HandlerList getHandlerList() {
return handlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,38 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.Snowball;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PreSnowballExplodeEvent extends Event implements Cancellable {
public class PreSnowballExplodeEvent extends SnowballExplodeEvent implements Cancellable {

private static final @NotNull HandlerList handlers = new HandlerList();
private boolean isCancelled;

private @NotNull Snowball snowball;
private @Nullable Entity hitEntity;
private @NotNull Location location;
private float explosionPower;
private boolean setFire, breakBlocks;

public PreSnowballExplodeEvent(
@NotNull Snowball snowball, @Nullable Entity hitEntity, @NotNull Location location,
float power, boolean setFire, boolean breakBlocks, boolean isAsync
) {
super(isAsync);
public PreSnowballExplodeEvent(@NotNull Snowball snowball, @Nullable Entity hitEntity,
@NotNull Location location, float power, boolean fire, boolean breakBlocks) {
super(snowball, hitEntity, location, power, fire, breakBlocks);
this.isCancelled = false;
this.snowball = snowball;
this.hitEntity = hitEntity;
this.location = location;
this.explosionPower = power;
this.setFire = setFire;
this.breakBlocks = breakBlocks;
}

public @NotNull Snowball getSnowball() {
return snowball;
}
public void setSnowball(@NotNull Snowball snowball) {
this.snowball = snowball;
}
public @Nullable Entity getHitEntity() {
return hitEntity;
}
public void setHitEntity(@Nullable Entity hitEntity) {
this.hitEntity = hitEntity;
}
public @NotNull Location getExplodeLocation() {
return location;
}
public void setExplodeLocation(@NotNull Location location) {

public void setLocation(@NotNull Location location) {
this.location = location;
}
public float getExplosionPower() {
return explosionPower;
}
public void setExplosionPower(float explosionPower) {

public void setPower(float explosionPower) {
this.explosionPower = explosionPower;
}
public boolean willSetFire() {
return setFire;
}

public void setFire(boolean setFire) {
this.setFire = setFire;
}
public boolean willBreakBlocks() {
return breakBlocks;
}

public void setBreakBlocks(boolean breakBlocks) {
this.breakBlocks = breakBlocks;
}
Expand All @@ -75,15 +44,18 @@ public void setBreakBlocks(boolean breakBlocks) {
public void setCancelled(boolean cancel) {
isCancelled = cancel;
}

@Override
public boolean isCancelled() {
return isCancelled;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

@NotNull
public static HandlerList getHandlerList() {
return handlers;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/me/xginko/snowballfight/events/SnowballEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.xginko.snowballfight.events;

import org.bukkit.entity.Snowball;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

public abstract class SnowballEvent extends Event {

private static final @NotNull HandlerList handlers = new HandlerList();

private final Snowball snowball;

public SnowballEvent(boolean isAsync, Snowball snowball) {
super(isAsync);
this.snowball = snowball;
}

public SnowballEvent(Snowball snowball) {
this.snowball = snowball;
}

public Snowball getSnowball() {
return snowball;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

@NotNull
public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.xginko.snowballfight.events;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Snowball;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class SnowballExplodeEvent extends SnowballEvent {

private static final @NotNull HandlerList handlers = new HandlerList();

protected @Nullable Entity hitEntity;
protected @NotNull Location location;
protected float explosionPower;
protected boolean setFire, breakBlocks;

public SnowballExplodeEvent(Snowball snowball, @Nullable Entity hitEntity,
@NotNull Location location, float explosionPower, boolean setFire, boolean breakBlocks) {
super(snowball);
this.hitEntity = hitEntity;
this.location = location;
this.explosionPower = explosionPower;
this.setFire = setFire;
this.breakBlocks = breakBlocks;
}

public @Nullable Entity getHitEntity() {
return hitEntity;
}

public @NotNull Location getLocation() {
return location;
}

public float getPower() {
return explosionPower;
}

public boolean getFire() {
return setFire;
}

public boolean getBreakBlocks() {
return breakBlocks;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

@NotNull
public static HandlerList getHandlerList() {
return handlers;
}
}
38 changes: 18 additions & 20 deletions src/main/java/me/xginko/snowballfight/modules/ExplodeOnHit.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ private void onProjectileHit(ProjectileHitEvent event) {
event.getEntity().getLocation(),
explosionPower,
explosionSetFire,
explosionBreakBlocks,
event.isAsynchronous()
explosionBreakBlocks
);

if (Util.isChunkUnsafe(
preSnowballExplodeEvent.getExplodeLocation().getBlockX() >> 4,
preSnowballExplodeEvent.getExplodeLocation().getBlockZ() >> 4)) {
preSnowballExplodeEvent.getLocation().getBlockX() >> 4,
preSnowballExplodeEvent.getLocation().getBlockZ() >> 4)) {
preSnowballExplodeEvent.setCancelled(true);
}

Expand All @@ -109,34 +108,33 @@ private void onProjectileHit(ProjectileHitEvent event) {
PostSnowballExplodeEvent postSnowballExplodeEvent = new PostSnowballExplodeEvent(
preSnowballExplodeEvent.getSnowball(),
preSnowballExplodeEvent.getHitEntity(),
preSnowballExplodeEvent.getExplodeLocation(),
preSnowballExplodeEvent.getExplosionPower(),
preSnowballExplodeEvent.willSetFire(),
preSnowballExplodeEvent.willBreakBlocks(),
createExplosion(preSnowballExplodeEvent),
event.isAsynchronous()
preSnowballExplodeEvent.getLocation(),
preSnowballExplodeEvent.getPower(),
preSnowballExplodeEvent.getFire(),
preSnowballExplodeEvent.getBreakBlocks(),
createExplosion(preSnowballExplodeEvent)
);

plugin.getServer().getPluginManager().callEvent(postSnowballExplodeEvent);
}

private boolean createExplosion(PreSnowballExplodeEvent preSnowballExplodeEvent) {
if (SnowballFight.isServerPaper()) {
return preSnowballExplodeEvent.getExplodeLocation().getWorld().createExplosion(
return preSnowballExplodeEvent.getLocation().getWorld().createExplosion(
// Set explode source for damage tracking without getting blocked by the MOB_GRIEFING gamerule
preSnowballExplodeEvent.getSnowball().getShooter() instanceof LivingEntity ?
(LivingEntity) preSnowballExplodeEvent.getSnowball().getShooter() : preSnowballExplodeEvent.getSnowball(),
preSnowballExplodeEvent.getExplodeLocation(),
preSnowballExplodeEvent.getExplosionPower(),
preSnowballExplodeEvent.willSetFire(),
preSnowballExplodeEvent.willBreakBlocks()
preSnowballExplodeEvent.getLocation(),
preSnowballExplodeEvent.getPower(),
preSnowballExplodeEvent.getFire(),
preSnowballExplodeEvent.getBreakBlocks()
);
} else {
return preSnowballExplodeEvent.getExplodeLocation().getWorld().createExplosion(
preSnowballExplodeEvent.getExplodeLocation(),
preSnowballExplodeEvent.getExplosionPower(),
preSnowballExplodeEvent.willSetFire(),
preSnowballExplodeEvent.willBreakBlocks()
return preSnowballExplodeEvent.getLocation().getWorld().createExplosion(
preSnowballExplodeEvent.getLocation(),
preSnowballExplodeEvent.getPower(),
preSnowballExplodeEvent.getFire(),
preSnowballExplodeEvent.getBreakBlocks()
);
}
}
Expand Down

0 comments on commit f67af6d

Please sign in to comment.