Skip to content

Commit 6266cb7

Browse files
authored
Remove TnT Countdown Bukkit implementation (#202)
1 parent 6dbf255 commit 6266cb7

File tree

7 files changed

+156
-297
lines changed

7 files changed

+156
-297
lines changed

api/src/main/java/com/lunarclient/apollo/module/tntcountdown/TntCountdownModule.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.lunarclient.apollo.module.ModuleDefinition;
2929
import com.lunarclient.apollo.option.NumberOption;
3030
import com.lunarclient.apollo.option.SimpleOption;
31+
import com.lunarclient.apollo.recipients.Recipients;
3132
import io.leangen.geantyref.TypeToken;
3233
import org.jetbrains.annotations.ApiStatus;
3334
import org.jetbrains.annotations.Range;
@@ -85,4 +86,16 @@ public boolean isClientNotify() {
8586
*/
8687
public abstract void setTntCountdown(ApolloEntity entity, @Range(from = 0, to = Integer.MAX_VALUE) int ticks);
8788

89+
/**
90+
* Set the amount of ticks before the specified TNT explodes.
91+
*
92+
* <p>The given ticks must be equal to or greater than 0.</p>
93+
*
94+
* @param recipients the recipients that are receiving the packet
95+
* @param entity the TNT entity
96+
* @param ticks the ticks until explosion
97+
* @since 1.1.8
98+
*/
99+
public abstract void setTntCountdown(Recipients recipients, ApolloEntity entity, @Range(from = 0, to = Integer.MAX_VALUE) int ticks);
100+
88101
}

bukkit-example-api/src/main/java/com/lunarclient/apollo/example/api/module/TntCountdownApiExample.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,69 @@
2525

2626
import com.lunarclient.apollo.Apollo;
2727
import com.lunarclient.apollo.common.ApolloEntity;
28+
import com.lunarclient.apollo.example.ApolloExamplePlugin;
2829
import com.lunarclient.apollo.example.module.impl.TntCountdownExample;
2930
import com.lunarclient.apollo.module.tntcountdown.TntCountdownModule;
30-
import com.lunarclient.apollo.player.ApolloPlayer;
31-
import java.util.Optional;
32-
import org.bukkit.Location;
33-
import org.bukkit.World;
31+
import com.lunarclient.apollo.recipients.Recipients;
32+
import org.bukkit.Bukkit;
3433
import org.bukkit.entity.Player;
3534
import org.bukkit.entity.TNTPrimed;
35+
import org.bukkit.event.EventHandler;
36+
import org.bukkit.event.EventPriority;
3637
import org.bukkit.event.Listener;
38+
import org.bukkit.event.entity.EntitySpawnEvent;
3739

3840
public class TntCountdownApiExample extends TntCountdownExample implements Listener {
3941

4042
private final TntCountdownModule tntCountdownModule = Apollo.getModuleManager().getModule(TntCountdownModule.class);
4143

44+
public TntCountdownApiExample() {
45+
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
46+
}
47+
4248
@Override
4349
public void setTntCountdownExample() {
4450
this.tntCountdownModule.getOptions().set(TntCountdownModule.TNT_TICKS, 160);
4551
}
4652

4753
@Override
4854
public void overrideTntCountdownExample(Player viewer) {
49-
Location location = viewer.getLocation();
50-
World world = viewer.getWorld();
51-
TNTPrimed entity = world.spawn(location, TNTPrimed.class);
55+
int customTicks = 200;
5256

53-
Optional<ApolloPlayer> apolloPlayerOpt = Apollo.getPlayerManager().getPlayer(viewer.getUniqueId());
57+
TNTPrimed entity = viewer.getWorld().spawn(viewer.getLocation(), TNTPrimed.class);
58+
entity.setFuseTicks(customTicks);
5459

55-
apolloPlayerOpt.ifPresent(apolloPlayer -> {
56-
ApolloEntity apolloEntity = new ApolloEntity(entity.getEntityId(), entity.getUniqueId());
57-
this.tntCountdownModule.setTntCountdown(apolloEntity, 200);
58-
});
60+
ApolloEntity apolloEntity = new ApolloEntity(entity.getEntityId(), entity.getUniqueId());
61+
this.tntCountdownModule.setTntCountdown(Recipients.ofEveryone(), apolloEntity, customTicks);
5962
}
6063

6164
@Override
6265
public void clearTntCountdownOptionExample() {
63-
this.tntCountdownModule.getOptions().remove(TntCountdownModule.TNT_TICKS, 80);
66+
this.tntCountdownModule.getOptions().remove(TntCountdownModule.TNT_TICKS, 160);
67+
}
68+
69+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
70+
private void onTntSpawn(EntitySpawnEvent event) {
71+
String entityName = event.getEntityType().name();
72+
if (!entityName.equals("PRIMED_TNT") && !entityName.equals("TNT")) {
73+
return;
74+
}
75+
76+
TNTPrimed primed = (TNTPrimed) event.getEntity();
77+
int customTicks = this.tntCountdownModule.getOptions().get(TntCountdownModule.TNT_TICKS);
78+
int defaultTicks = TntCountdownModule.TNT_TICKS.getDefaultValue();
79+
int currentTicks = primed.getFuseTicks();
80+
81+
if (currentTicks != defaultTicks && !this.tntCountdownModule.getOptions().get(TntCountdownModule.OVERRIDE_CUSTOM_TICKS)) {
82+
customTicks = currentTicks;
83+
84+
this.tntCountdownModule.setTntCountdown(Recipients.ofEveryone(),
85+
new ApolloEntity(primed.getEntityId(), primed.getUniqueId()),
86+
customTicks
87+
);
88+
}
89+
90+
primed.setFuseTicks(customTicks);
6491
}
6592

6693
}

bukkit-example-json/src/main/java/com/lunarclient/apollo/example/json/module/TntCountdownJsonExample.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@
2828
import com.lunarclient.apollo.example.json.util.JsonPacketUtil;
2929
import com.lunarclient.apollo.example.json.util.JsonUtil;
3030
import com.lunarclient.apollo.example.module.impl.TntCountdownExample;
31-
import java.lang.reflect.Method;
3231
import java.util.HashMap;
3332
import java.util.Map;
34-
import java.util.UUID;
3533
import org.bukkit.Bukkit;
36-
import org.bukkit.Location;
37-
import org.bukkit.World;
3834
import org.bukkit.entity.Entity;
3935
import org.bukkit.entity.Player;
4036
import org.bukkit.entity.TNTPrimed;
@@ -45,16 +41,6 @@
4541

4642
public class TntCountdownJsonExample extends TntCountdownExample implements Listener {
4743

48-
private static Method entityGetter;
49-
50-
static {
51-
try {
52-
TntCountdownJsonExample.entityGetter = Bukkit.class.getDeclaredMethod("getEntity", UUID.class);
53-
} catch (Throwable throwable) {
54-
// Ignore for legacy versions.
55-
}
56-
}
57-
5844
public TntCountdownJsonExample() {
5945
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
6046
}
@@ -70,31 +56,10 @@ public void setTntCountdownExample() {
7056

7157
@Override
7258
public void overrideTntCountdownExample(Player viewer) {
73-
Location location = viewer.getLocation();
74-
TNTPrimed entity = viewer.getWorld().spawn(location, TNTPrimed.class);
7559
int customTicks = 200;
7660

77-
TNTPrimed target = null;
78-
if (TntCountdownJsonExample.entityGetter != null) {
79-
try {
80-
target = (TNTPrimed) TntCountdownJsonExample.entityGetter.invoke(null, entity.getUniqueId());
81-
} catch (Throwable throwable) {
82-
throwable.printStackTrace();
83-
}
84-
} else {
85-
for (World world : Bukkit.getWorlds()) {
86-
for (TNTPrimed compare : world.getEntitiesByClass(TNTPrimed.class)) {
87-
if (compare.getUniqueId().equals(entity.getUniqueId())) {
88-
target = compare;
89-
break;
90-
}
91-
}
92-
}
93-
}
94-
95-
if (target != null) {
96-
target.setFuseTicks(customTicks);
97-
}
61+
TNTPrimed entity = viewer.getWorld().spawn(viewer.getLocation(), TNTPrimed.class);
62+
entity.setFuseTicks(customTicks);
9863

9964
JsonPacketUtil.sendPacket(viewer, this.createTNTCountdownMessage(entity, customTicks));
10065
}

bukkit-example-proto/src/main/java/com/lunarclient/apollo/example/proto/module/TntCountdownProtoExample.java

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@
3030
import com.lunarclient.apollo.example.proto.util.ProtobufPacketUtil;
3131
import com.lunarclient.apollo.example.proto.util.ProtobufUtil;
3232
import com.lunarclient.apollo.tntcountdown.v1.SetTntCountdownMessage;
33-
import java.lang.reflect.Method;
3433
import java.util.HashMap;
3534
import java.util.Map;
36-
import java.util.UUID;
3735
import org.bukkit.Bukkit;
38-
import org.bukkit.Location;
39-
import org.bukkit.World;
4036
import org.bukkit.entity.Player;
4137
import org.bukkit.entity.TNTPrimed;
4238
import org.bukkit.event.EventHandler;
@@ -46,16 +42,6 @@
4642

4743
public class TntCountdownProtoExample extends TntCountdownExample implements Listener {
4844

49-
private static Method entityGetter;
50-
51-
static {
52-
try {
53-
TntCountdownProtoExample.entityGetter = Bukkit.class.getDeclaredMethod("getEntity", UUID.class);
54-
} catch (Throwable throwable) {
55-
// Ignore for legacy versions.
56-
}
57-
}
58-
5945
public TntCountdownProtoExample() {
6046
Bukkit.getPluginManager().registerEvents(this, ApolloExamplePlugin.getInstance());
6147
}
@@ -71,44 +57,23 @@ public void setTntCountdownExample() {
7157

7258
@Override
7359
public void overrideTntCountdownExample(Player viewer) {
74-
Location location = viewer.getLocation();
75-
TNTPrimed entity = viewer.getWorld().spawn(location, TNTPrimed.class);
7660
int customTicks = 200;
7761

78-
TNTPrimed target = null;
79-
if (TntCountdownProtoExample.entityGetter != null) {
80-
try {
81-
target = (TNTPrimed) TntCountdownProtoExample.entityGetter.invoke(null, entity.getUniqueId());
82-
} catch (Throwable throwable) {
83-
throwable.printStackTrace();
84-
}
85-
} else {
86-
for (World world : Bukkit.getWorlds()) {
87-
for (TNTPrimed compare : world.getEntitiesByClass(TNTPrimed.class)) {
88-
if (compare.getUniqueId().equals(entity.getUniqueId())) {
89-
target = compare;
90-
break;
91-
}
92-
}
93-
}
94-
}
95-
96-
if (target != null) {
97-
target.setFuseTicks(customTicks);
98-
}
62+
TNTPrimed entity = viewer.getWorld().spawn(viewer.getLocation(), TNTPrimed.class);
63+
entity.setFuseTicks(customTicks);
9964

10065
SetTntCountdownMessage message = SetTntCountdownMessage.newBuilder()
10166
.setEntityId(ProtobufUtil.createEntityIdProto(entity.getEntityId(), entity.getUniqueId()))
10267
.setDurationTicks(customTicks)
10368
.build();
10469

105-
ProtobufPacketUtil.sendPacket(viewer, message);
70+
ProtobufPacketUtil.broadcastPacket(message);
10671
}
10772

10873
@Override
10974
public void clearTntCountdownOptionExample() {
11075
Map<String, Value> properties = new HashMap<>();
111-
properties.put("tnt-ticks", Value.newBuilder().setNumberValue(80).build());
76+
properties.put("tnt-ticks", Value.newBuilder().setNumberValue(160).build());
11277

11378
ConfigurableSettings settings = ProtobufPacketUtil.createModuleMessage("tnt_countdown", properties);
11479
ProtobufPacketUtil.broadcastPacket(settings);

bukkit/src/main/java/com/lunarclient/apollo/module/tntcountdown/TntCountdownModuleImpl.java

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)