Skip to content

Commit 891a2f8

Browse files
committed
Refactor player access and add entity spawn permission checks
Replaced direct `player` field access with `getPlatformPlayer()` calls for improved abstraction and consistency. Added permission validation for spawning entities via Axiom packets to ensure proper access control. Minor formatting and code cleanup were also performed for maintainability.
1 parent 0d28055 commit 891a2f8

13 files changed

+169
-153
lines changed

Fabric/src/main/java/com/plotsquared/fabric/FabricPlatform.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
import net.minecraft.commands.CommandSourceStack;
120120
import net.minecraft.commands.Commands;
121121
import net.minecraft.core.GlobalPos;
122+
import net.minecraft.resources.ResourceLocation;
122123
import net.minecraft.server.MinecraftServer;
123124
import net.minecraft.server.level.ServerLevel;
124125
import net.minecraft.server.level.ServerPlayer;
@@ -173,7 +174,7 @@ public class FabricPlatform implements ModInitializer, PlotPlatform<ServerPlayer
173174
private boolean faweHook = false;
174175

175176
private static CraftScheduler scheduler;
176-
177+
public PlotSquared plotSquared;
177178
private Injector injector;
178179

179180
@Inject
@@ -262,7 +263,7 @@ public void onInitialize() {
262263
getScheduler().mainThreadHeartbeat(server.getTickCount());
263264
});
264265

265-
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
266+
ServerLifecycleEvents.SERVER_STARTED.register( server -> {
266267
SERVER = server;
267268
this.pluginName = "PlotSquared";
268269

@@ -273,7 +274,7 @@ public void onInitialize() {
273274
PlotPlayer.registerConverter(ServerPlayer.class, FabricUtil::adapt);
274275
TaskManager.setPlatformImplementation(new FabricTaskManager(this, timeConverter));
275276

276-
final PlotSquared plotSquared = new PlotSquared(this, "Fabric");
277+
plotSquared = new PlotSquared(this, "Fabric");
277278

278279
// FastAsyncWorldEdit
279280
if (Settings.FAWE_Components.FAWE_HOOK) {

Fabric/src/main/java/com/plotsquared/fabric/listener/BlockEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static void sendBlockChange(final GlobalPos bloc, final net.minecraft.wor
182182
if (16 * Math.abs(location.getX() - x) / 16 > distance || 16 * Math.abs(location.getZ() - z) / 16 > distance) {
183183
continue;
184184
}
185-
((FabricPlayer) player).player.serverLevel().blockUpdated(bloc.pos(), data.getBlock());
185+
((FabricPlayer) player).getPlatformPlayer().serverLevel().blockUpdated(bloc.pos(), data.getBlock());
186186
}
187187
}
188188
}, TaskTime.ticks(3L));

Fabric/src/main/java/com/plotsquared/fabric/listener/ChunkListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public InteractionResult onEntitySpawn(Entity entity) {
242242
entity.remove(Entity.RemovalReason.DISCARDED);
243243
return InteractionResult.FAIL;
244244
}
245-
if (!this.plotAreaManager.hasPlotArea(serverLevel.dimension().location().getPath().toString())) {
245+
if (!this.plotAreaManager.hasPlotArea(serverLevel.dimension().location().getPath())) {
246246
return InteractionResult.PASS;
247247
}
248248
Entity[] entities = FabricUtil.getEntitiesInChunk(serverLevel, chunk).toArray(new Entity[0]);

Fabric/src/main/java/com/plotsquared/fabric/listener/EntitySpawnListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package com.plotsquared.fabric.listener;
2020

21+
import com.moulberry.axiom.packets.AxiomServerboundSpawnEntity;
2122
import com.plotsquared.core.PlotSquared;
2223
import com.plotsquared.core.configuration.Settings;
2324
import com.plotsquared.core.location.Location;

Fabric/src/main/java/com/plotsquared/fabric/listener/ForceFieldListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static void handleForcefield(ServerPlayer player, PlotPlayer<?> plotPlaye
110110
Set<PlotPlayer<?>> players = getNearbyPlayers(player, plot);
111111
for (PlotPlayer<?> oPlayer : players) {
112112
if (!oPlayer.hasPermission(Permission.PERMISSION_ADMIN_ENTRY_FORCEFIELD)) {
113-
((FabricPlayer) oPlayer).player
113+
((FabricPlayer) oPlayer).getPlatformPlayer()
114114
.setDeltaMovement(calculateVelocity(plotPlayer, oPlayer));
115115
}
116116
}

Fabric/src/main/java/com/plotsquared/fabric/listener/HighFreqBlockEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static void sendBlockChange(GlobalPos bloc, final BlockState data) {
9494
if (16 * Math.abs(location.getX() - x) / 16 > distance || 16 * Math.abs(location.getZ() - z) / 16 > distance) {
9595
continue;
9696
}
97-
((FabricPlayer) player).player.serverLevel().blockUpdated(bloc.pos(), data.getBlock());
97+
((FabricPlayer) player).getPlatformPlayer().serverLevel().blockUpdated(bloc.pos(), data.getBlock());
9898
}
9999
}
100100
}, TaskTime.ticks(3L));

Fabric/src/main/java/com/plotsquared/fabric/listener/PlayerEventListener.java

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@
8989
import com.sk89q.worldedit.world.entity.EntityType;
9090
import com.sk89q.worldedit.world.entity.EntityTypes;
9191
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
92-
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
9392
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
94-
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
9593
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
9694
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
9795
import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents;
@@ -280,8 +278,11 @@ public PlayerEventListener(
280278
PlayerBlockBreakEvents.AFTER.register(this::afterBlockBreak);
281279
UseBlockCallback.EVENT.register(PlayerEventListener::interact);
282280
Stimuli.global().listen(PlayerCommandEvent.EVENT, PlayerEventListener::onPlayerCommand);
281+
283282
ServerPlayConnectionEvents.INIT.register(PlayerEventListener::onLoginInit);
283+
284284
Stimuli.global().listen(BlockUseEvent.EVENT, this::onHangingPlace);
285+
285286
Stimuli.global().listen(EntitySpawnEvent.EVENT, entity -> {
286287
Location location = FabricUtil.adapt(GlobalPos.of(entity.level().dimension(), entity.blockPosition()));
287288
PlotArea area = location.getPlotArea();
@@ -296,50 +297,14 @@ public PlayerEventListener(
296297
}
297298
return InteractionResult.PASS;
298299
});
300+
299301
Stimuli.global().listen(EntityDeathEvent.EVENT, this::onHangingBreakByEntity);
300302
Stimuli.global().listen(EntityUseEvent.EVENT, this::onPlayerInteractEntity);
301303
Stimuli.global().listen(EntityDeathEvent.EVENT, this::onVehicleDestroy);
302304
Stimuli.global().listen(EndPortalOpenEvent.EVENT, this::onEndPortalCreation);
303305
BaseFireBlockOnPlaceCallback.EVENT.register(this::onNetherPortalCreation);
304306
EntityHandleInsidePortalCallback.EVENT.register(this::onPortalEnter);
305307
LecternTakeButtonCallback.EVENT.register(this::onPlayerTakeLecternBook);
306-
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
307-
TaskManager.runTaskLater(() -> {
308-
if (entity instanceof ServerPlayer serverPlayer) {
309-
final ServerPlayer player = serverPlayer;
310-
PlotSquared.platform().playerManager().removePlayer(player.getUUID());
311-
final PlotPlayer<ServerPlayer> pp = FabricUtil.adapt(player);
312-
313-
// we're stripping the country code as we don't want to differ between countries
314-
//pp.setLocale(Locale.forLanguageTag(player.getLocale().substring(0, 2)));
315-
316-
Location location = pp.getLocation();
317-
PlotArea area = location.getPlotArea();
318-
if (area != null) {
319-
Plot plot = area.getPlot(location);
320-
if (plot != null) {
321-
plotListener.plotEntry(pp, plot);
322-
}
323-
}
324-
// Async
325-
TaskManager.runTaskLaterAsync(() -> {
326-
/* TODO CHECK ON THIS */
327-
/*if (!player.hasPlayedBefore() && player.isLocalPlayer()) {
328-
player.saveData();
329-
}*/
330-
this.eventDispatcher.doJoinTask(pp);
331-
}, TaskTime.seconds(1L));
332-
}
333-
}, TaskTime.seconds(3L));
334-
335-
});
336-
ServerLoginConnectionEvents.DISCONNECT.register(this::onLeave);
337-
ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> {
338-
ServerPlayer player = newPlayer;
339-
PlotPlayer<ServerPlayer> pp = FabricUtil.adapt(player);
340-
this.eventDispatcher.doRespawnTask(pp);
341-
});
342-
343308
HandlePlayerMoveCallback.EVENT.register((serverboundMovePlayerPacket, serverPlayer) -> {
344309
if (!serverboundMovePlayerPacket.hasPosition()) {
345310
return InteractionResult.PASS;
@@ -361,11 +326,11 @@ public PlayerEventListener(
361326
if (MathMan.roundInt(from.pos().getX()) != (x2 = MathMan.roundInt(to.pos().getX()))) {
362327
ServerPlayer player = serverPlayer;
363328
FabricPlayer pp;
364-
try {
365-
pp = FabricUtil.adapt(player);
366-
} catch (Exception e ) {
367-
return InteractionResult.PASS;
368-
}
329+
try {
330+
pp = FabricUtil.adapt(player);
331+
} catch (Exception e) {
332+
return InteractionResult.PASS;
333+
}
369334
// Cancel teleport
370335
if (TaskManager.removeFromTeleportQueue(pp.getName())) {
371336
pp.sendMessage(TranslatableCaption.of("teleport.teleport_failed"));
@@ -562,6 +527,43 @@ public PlayerEventListener(
562527
}
563528
return InteractionResult.PASS;
564529
});
530+
531+
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
532+
TaskManager.runTaskLater(() -> {
533+
final ServerPlayer player = handler.player;
534+
PlotSquared.platform().playerManager().removePlayer(player.getUUID());
535+
final PlotPlayer<ServerPlayer> pp = FabricUtil.adapt(player);
536+
537+
// we're stripping the country code as we don't want to differ between countries
538+
//pp.setLocale(Locale.forLanguageTag(player.getLocale().substring(0, 2)));
539+
540+
Location location = pp.getLocation();
541+
PlotArea area = location.getPlotArea();
542+
if (area != null) {
543+
Plot plot = area.getPlot(location);
544+
if (plot != null) {
545+
plotListener.plotEntry(pp, plot);
546+
}
547+
}
548+
// Async
549+
TaskManager.runTaskLaterAsync(() -> {
550+
/* TODO CHECK ON THIS */
551+
/*if (!player.hasPlayedBefore() && player.isLocalPlayer()) {
552+
player.saveData();
553+
}*/
554+
this.eventDispatcher.doJoinTask(pp);
555+
}, TaskTime.seconds(1L));
556+
}, TaskTime.seconds(3L));
557+
558+
});
559+
ServerPlayConnectionEvents.DISCONNECT.register(this::onLeave);
560+
561+
ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> {
562+
PlotPlayer<ServerPlayer> pp = FabricUtil.adapt(newPlayer);
563+
this.eventDispatcher.doRespawnTask(pp);
564+
});
565+
566+
565567
ServerPlayerTeleportToCallback.EVENT.register((serverLevel, x, y, z, set, g, h, serverPlayer) -> {
566568
ServerPlayer player = serverPlayer;
567569
//We need to account for bad plugins like NoCheatPlus that teleports player on/before login -_-
@@ -749,8 +751,10 @@ public PlayerEventListener(
749751
//cancel the original message
750752
return InteractionResult.FAIL;
751753
});
754+
752755
ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.register((player, origin, destination) -> {
753-
TaskManager.runTaskLater(() -> {FabricPlayer pp = FabricUtil.adapt(player);
756+
TaskManager.runTaskLater(() -> {
757+
FabricPlayer pp = FabricUtil.adapt(player);
754758
// Delete last location
755759
Plot plot;
756760
try (final MetaDataAccess<Plot> lastPlotAccess =
@@ -778,7 +782,8 @@ public PlayerEventListener(
778782
if (plot != null) {
779783
plotListener.plotEntry(pp, plot);
780784
}
781-
}}, TaskTime.seconds(3));
785+
}
786+
}, TaskTime.seconds(3));
782787

783788
});
784789
Stimuli.global().listen(PlayerInventoryActionEvent.EVENT, (serverPlayer, i, clickType, i1) -> {
@@ -1529,9 +1534,9 @@ public InteractionResult onInventoryClose(
15291534
return InteractionResult.PASS;
15301535
}
15311536

1532-
public void onLeave(ServerLoginPacketListenerImpl handler, MinecraftServer server) {
1533-
TaskManager.removeFromTeleportQueue(handler.getUserName());
1534-
FabricPlayer pp = FabricUtil.adapt(FabricPlatform.SERVER.getPlayerList().getPlayerByName(handler.getUserName()));
1537+
public void onLeave(ServerGamePacketListenerImpl handler, MinecraftServer server) {
1538+
TaskManager.removeFromTeleportQueue(handler.player.getName().getString());
1539+
FabricPlayer pp = FabricUtil.adapt(handler.player);
15351540
pp.unregister();
15361541
plotListener.logout(pp.getUUID());
15371542
}
@@ -1594,7 +1599,10 @@ public InteractionResult onBucketFill(Player player, Level level, InteractionHan
15941599
}
15951600

15961601
public InteractionResult onHangingPlace(ServerPlayer player, InteractionHand hand, BlockHitResult hitResult) {
1597-
if (player.getUseItem().getItem() instanceof HangingEntityItem || player.getUseItem().getItem().equals(Items.PAINTING) || player.getUseItem().getItem() instanceof LeadItem) {
1602+
if (player.getUseItem().getItem() instanceof HangingEntityItem || player
1603+
.getUseItem()
1604+
.getItem()
1605+
.equals(Items.PAINTING) || player.getUseItem().getItem() instanceof LeadItem) {
15981606
Block block = player.serverLevel().getBlockState(hitResult.getBlockPos()).getBlock();
15991607
Location location = FabricUtil.adapt(GlobalPos.of(player.serverLevel().dimension(), hitResult.getBlockPos()));
16001608
PlotArea area = location.getPlotArea();
@@ -1786,7 +1794,9 @@ public InteractionResult onPlayerInteractEntity(
17861794
}
17871795
} else if ((plot != null && !plot.isAdded(pp.getUUID())) || (plot == null && area
17881796
.isRoadFlags())) {
1789-
final com.sk89q.worldedit.world.entity.EntityType entityType = EntityType.REGISTRY.get(entity.getType().toShortString());
1797+
final com.sk89q.worldedit.world.entity.EntityType entityType = EntityType.REGISTRY.get(entity
1798+
.getType()
1799+
.toShortString());
17901800

17911801
FlagContainer flagContainer;
17921802
if (plot == null) {

Fabric/src/main/java/com/plotsquared/fabric/listener/ProjectileEventListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public InteractionResult onProjectileHit(Projectile entity, BlockHitResult block
153153
}
154154
Plot plot = area.getPlot(location);
155155
Entity shooter = entity.getOwner();
156+
if (shooter == null) {
157+
return InteractionResult.FAIL;
158+
}
156159
if (shooter instanceof ServerPlayer player) {
157160
if (!(player.connection.isAcceptingMessages())) {
158161
if (plot != null) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.plotsquared.fabric.listener.mixin;
2+
3+
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
5+
import com.moulberry.axiom.packets.AxiomServerboundSpawnEntity;
6+
import net.luckperms.api.LuckPermsProvider;
7+
import net.minecraft.server.MinecraftServer;
8+
import net.minecraft.server.level.ServerPlayer;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
11+
@Mixin(AxiomServerboundSpawnEntity.class)
12+
public class AxiomServerboundSpawnEntityMixin {
13+
14+
15+
@WrapMethod(method = "handle")
16+
public void onHandle(MinecraftServer server, ServerPlayer player, Operation<Void> original){
17+
if(LuckPermsProvider.get().getUserManager().getUser(player.getUUID()).getCachedData().getPermissionData().checkPermission("axiom.entity.spawn").asBoolean()) {
18+
original.call(server, player);
19+
}
20+
}
21+
22+
}

Fabric/src/main/java/com/plotsquared/fabric/placeholder/PlaceholderFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void format(final @NonNull ChatContext context) {
3434
if (context.isRawOutput()) {
3535
context.setMessage(context.getMessage().replace('%', '\u2010'));
3636
} else {
37-
final ServerPlayer player = ((FabricPlayer) recipient).player;
37+
final ServerPlayer player = ((FabricPlayer) recipient).getPlatformPlayer();
3838
// context.setMessage(MiniPlaceholders.);
3939
//context.setMessage(PlaceholderAPI.setPlaceholders(player, context.getMessage()));
4040
}

0 commit comments

Comments
 (0)