Skip to content

Commit

Permalink
Add long render distance tnt
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 authored and Electroid committed Apr 20, 2021
1 parent a4b07a5 commit a3992e8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import tc.oc.pgm.timelimit.TimeLimitModule;
import tc.oc.pgm.tnt.TNTMatchModule;
import tc.oc.pgm.tnt.TNTModule;
import tc.oc.pgm.tntrender.TNTRenderMatchModule;
import tc.oc.pgm.tracker.TrackerMatchModule;
import tc.oc.pgm.wool.WoolMatchModule;
import tc.oc.pgm.wool.WoolModule;
Expand Down Expand Up @@ -157,6 +158,7 @@ static void registerAll() {
register(FireworkMatchModule.class, FireworkMatchModule::new);
register(StatsMatchModule.class, StatsMatchModule::new);
register(MapmakerMatchModule.class, MapmakerMatchModule::new);
register(TNTRenderMatchModule.class, TNTRenderMatchModule::new);

// FIXME: Disabled due to lag - look into future optimization
// register(ProjectileTrailMatchModule.class, ProjectileTrailMatchModule::new);
Expand Down
102 changes: 102 additions & 0 deletions core/src/main/java/tc/oc/pgm/tntrender/TNTRenderMatchModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package tc.oc.pgm.tntrender;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchModule;
import tc.oc.pgm.api.match.MatchScope;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.util.nms.NMSHacks;

public class TNTRenderMatchModule implements MatchModule, Listener {

private static final double MAX_DISTANCE = Math.pow(64d, 2);

private final Match match;
private final List<PrimedTnt> entities;

public TNTRenderMatchModule(Match match) {
this.match = match;
this.entities = new ArrayList<>();
}

@Override
public void enable() {
match
.getExecutor(MatchScope.LOADED)
.scheduleAtFixedRate(
() -> entities.removeIf(PrimedTnt::update), 0, 50, TimeUnit.MILLISECONDS);
}

@EventHandler
public void onTntSpawn(ExplosionPrimeEvent event) {
if (event.getEntity() instanceof TNTPrimed)
entities.add(new PrimedTnt((TNTPrimed) event.getEntity()));
}

@EventHandler
public void onTntExplode(EntityExplodeEvent event) {
if (!(event.getEntity() instanceof TNTPrimed)) return;
Location explosion = event.getLocation();
for (MatchPlayer player : match.getPlayers()) {
if (explosion.distanceSquared(player.getBukkit().getLocation()) >= MAX_DISTANCE)
player
.getBukkit()
.spigot()
.playEffect(explosion, Effect.EXPLOSION_HUGE, 0, 0, 0f, 0f, 0f, 1f, 1, 256);
}
}

private class PrimedTnt {
private final TNTPrimed entity;
private final Set<MatchPlayer> viewers = new HashSet<>();
private Location lastLocation, currentLocation;
private boolean moved = false;

public PrimedTnt(TNTPrimed entity) {
this.entity = entity;
this.lastLocation = currentLocation = entity.getLocation().toBlockLocation();
}

public boolean update() {
if (entity.isDead()) {
for (MatchPlayer viewer : viewers)
NMSHacks.sendBlockChange(currentLocation, viewer.getBukkit(), null);
return true;
}

this.lastLocation = currentLocation;
this.currentLocation = entity.getLocation().toBlockLocation();
this.moved = !currentLocation.equals(lastLocation);

for (MatchPlayer player : match.getPlayers()) {
updatePlayer(player);
}
return false;
}

private void updatePlayer(MatchPlayer player) {
if (currentLocation.distanceSquared(player.getBukkit().getLocation()) >= MAX_DISTANCE) {
if (viewers.add(player)) {
NMSHacks.sendBlockChange(currentLocation, player.getBukkit(), Material.TNT);
} else if (moved) {
NMSHacks.sendBlockChange(lastLocation, player.getBukkit(), null);
NMSHacks.sendBlockChange(currentLocation, player.getBukkit(), Material.TNT);
}
} else if (viewers.remove(player)) {
NMSHacks.sendBlockChange(lastLocation, player.getBukkit(), null);
}
}
}
}
5 changes: 5 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,11 @@ static void updateChunkSnapshot(ChunkSnapshot snapshot, org.bukkit.block.BlockSt
}
}

static void sendBlockChange(Location loc, Player player, @Nullable Material material) {
if (material != null) player.sendBlockChange(loc, material, (byte) 0);
else player.sendBlockChange(loc, loc.getBlock().getType(), loc.getBlock().getData());
}

interface FakeEntity {
int entityId();

Expand Down

0 comments on commit a3992e8

Please sign in to comment.