Skip to content

Commit

Permalink
Fix events running on the client side
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 17, 2024
1 parent e80f09b commit 9111dd0
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 10 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ dependencies {
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
modImplementation include("eu.pb4:placeholder-api:${project.placeholder_version}")
modImplementation include("xyz.nucleoid:server-translations-api:${project.stapi_version}")
modImplementation include("me.lucko:fabric-permissions-api:0.3.2-SNAPSHOT")
//modImplementation include("com.github.sakura-ryoko:fabric-permissions-api:dev~snapshot-jitpack-SNAPSHOT")
modImplementation include("me.lucko:fabric-permissions-api:0.3.3")
modImplementation include("eu.pb4:common-protection-api:1.0.0")

compileOnly "us.dynmap:DynmapCoreAPI:${dynmap_version}"
Expand Down
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.21.2
yarn_mappings=1.21.2+build.1
loader_version=0.16.7
minecraft_version=1.21.3
yarn_mappings=1.21.3+build.1
loader_version=0.16.9

#Fabric api
fabric_version=0.106.0+1.21.2
fabric_version=0.106.1+1.21.3

# Mod Properties
mod_version = 1.14.0
mod_version = 1.14.1
maven_group = eu.pb4
archives_base_name = goml

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
rtree_version=0.3.0
cca_version=6.1.1
cca_version=6.2.0
pal_version=1.10.0
polymer_version=0.10.0+1.21.2
polymer_version=0.10.2+1.21.3
sgui_version=1.7.2+1.21.2
stapi_version=2.4.0+1.21.2-rc1
placeholder_version=2.5.0+1.21.2
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/draylar/goml/EventHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static void init() {

private static void registerInteractEntityCallback() {
UseEntityCallback.EVENT.register(GOML_PHASE, (playerEntity, world, hand, entity, entityHitResult) -> {
if (world.isClient) {
return ActionResult.PASS;
}

if (ClaimUtils.isInAdminMode(playerEntity)) {
return ActionResult.PASS;
}
Expand Down Expand Up @@ -109,12 +113,18 @@ private static void registerInteractEntityCallback() {

private static void registerAttackEntityCallback() {
AttackEntityCallback.EVENT.register(GOML_PHASE, (playerEntity, world, hand, entity, entityHitResult) -> {
if (world.isClient) {
return ActionResult.PASS;
}
return ClaimUtils.canDamageEntity(world, entity, world.getDamageSources().playerAttack(playerEntity)) ? ActionResult.PASS : ActionResult.FAIL;
});
}

private static void registerInteractBlockCallback() {
UseBlockCallback.EVENT.register(GOML_PHASE, (playerEntity, world, hand, blockHitResult) -> {
if (world.isClient) {
return ActionResult.PASS;
}
if (!(playerEntity.getStackInHand(hand).getItem() instanceof BlockItem)) {
var blockState = world.getBlockState(blockHitResult.getBlockPos());

Expand All @@ -138,11 +148,17 @@ private static void registerInteractBlockCallback() {

private static void registerBreakBlockCallback() {
AttackBlockCallback.EVENT.register(GOML_PHASE, (playerEntity, world, hand, blockPos, direction) -> {
if (world.isClient) {
return ActionResult.PASS;
}
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(world, blockPos);
return testPermission(claimsFound, playerEntity, hand, blockPos, PermissionReason.BLOCK_PROTECTED);
});

PlayerBlockBreakEvents.BEFORE.register(GOML_PHASE, (world, player, pos, state, blockEntity) -> {
if (world.isClient) {
return true;
}
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(world, pos);
ActionResult result = testPermission(claimsFound, player, Hand.MAIN_HAND, pos, PermissionReason.BLOCK_PROTECTED);
return !result.equals(ActionResult.FAIL);
Expand All @@ -151,6 +167,9 @@ private static void registerBreakBlockCallback() {

private static void registerAnchorAttackCallback() {
AttackBlockCallback.EVENT.register(GOML_PHASE, (playerEntity, world, hand, blockPos, direction) -> {
if (world.isClient) {
return ActionResult.PASS;
}
var be = world.getBlockEntity(blockPos);

if (be instanceof ClaimAnchorBlockEntity) {
Expand All @@ -165,6 +184,10 @@ private static void registerAnchorAttackCallback() {

@ApiStatus.Internal
public static ActionResult testPermission(Selection<Entry<ClaimBox, Claim>> claims, PlayerEntity player, Hand hand, BlockPos pos, PermissionReason reason) {
if (player.getWorld().isClient) {
return ActionResult.PASS;
}

if (!claims.isEmpty()) {
boolean noPermission = claims.anyMatch((Entry<ClaimBox, Claim> boxInfo) -> !boxInfo.getValue().hasPermission(player));

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/draylar/goml/api/ClaimUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public static boolean canExplosionDestroy(World world, BlockPos pos, @Nullable E
}

public static boolean canDamageEntity(World world, Entity entity, DamageSource source) {
if (world.isClient) {
return true;
}

if (entity == source.getAttacker()) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/draylar/goml/mixin/BucketItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public BucketItemMixin(Settings settings) {

@Inject(at = @At("HEAD"), method = "use", cancellable = true)
private void goml_preventBucketUsageInClaims(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
if (world.isClient) {
return;
}

HitResult hitResult = raycast(world, user, this.fluid == Fluids.EMPTY ? RaycastContext.FluidHandling.SOURCE_ONLY : RaycastContext.FluidHandling.NONE);
BlockHitResult blockHitResult = (BlockHitResult) hitResult;
BlockPos blockPos = blockHitResult.getBlockPos();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/draylar/goml/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public abstract class EntityMixin implements OriginOwner {

@Inject(method = "isAlwaysInvulnerableTo", at = @At("HEAD"), cancellable = true)
private void goml$isInvulnerable(DamageSource damageSource, CallbackInfoReturnable<Boolean> cir) {
if (this.world.isClient) {
return;
}

if (!ClaimUtils.canDamageEntity(this.world, (Entity) (Object) this, damageSource)) {
cir.setReturnValue(true);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/draylar/goml/mixin/FallingBlockEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public FallingBlockEntityMixin(EntityType<?> type, World world) {

@ModifyExpressionValue(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;canPlaceAt(Lnet/minecraft/world/WorldView;Lnet/minecraft/util/math/BlockPos;)Z"))
private boolean cantPlaceOnClaim(boolean bool, @Local(ordinal = 0) BlockPos pos) {
if (this.getWorld().isClient) {
return bool;
}
if (bool) {
return ClaimUtils.hasMatchingClaims(this.getWorld(), pos, this.goml$getOriginSafe());
}
Expand All @@ -36,6 +39,9 @@ private boolean cantPlaceOnClaim(boolean bool, @Local(ordinal = 0) BlockPos pos)

@Inject(method = "handleFallDamage", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/MathHelper;ceil(F)I", ordinal = 0), cancellable = true)
private void blockFallDamage(float fallDistance, float damageMultiplier, DamageSource damageSource, CallbackInfoReturnable<Boolean> cir) {
if (this.getWorld().isClient) {
return;
}
if (!ClaimUtils.hasMatchingClaims(this.getWorld(), this.getBlockPos(), this.goml$getOriginSafe())) {
cir.setReturnValue(false);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/draylar/goml/mixin/FarmlandBlockMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public FarmlandBlockMixin(Settings settings) {

@Inject(method = "setToDirt", at = @At("HEAD"), cancellable = true)
private static void goml$protectFarmland(Entity entity, BlockState state, World world, BlockPos pos, CallbackInfo ci) {
if (world.isClient) {
return;
}
if (!ClaimUtils.canModify(world, pos, entity instanceof PlayerEntity player ? player : null)) {
ci.cancel();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/draylar/goml/mixin/FireBlockMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
public class FireBlockMixin {
@Inject(method = "trySpreadingFire", at = @At("HEAD"), cancellable = true)
private void goml_preventFire(World world, BlockPos pos, int spreadFactor, Random random, int currentAge, CallbackInfo ci) {
if (world.isClient) {
return;
}
if (!ClaimUtils.canFireDestroy(world, pos)) {
ci.cancel();
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/draylar/goml/mixin/PistonBlockMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public abstract class PistonBlockMixin {

@Inject(at = @At("HEAD"), method = "isMovable", cancellable = true)
private static void goml_isMoveable(BlockState state, World world, BlockPos pos, Direction motionDir, boolean canBreak, Direction pistonDir, CallbackInfoReturnable<Boolean> cir) {
if (world.isClient) {
return;
}

if(state.getBlock() instanceof ClaimAnchorBlock) {
cir.setReturnValue(false);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/draylar/goml/mixin/PistonHandlerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class PistonHandlerMixin {

@Inject(method = "<init>", at = @At("TAIL"))
private void storeClaimInfo(World world, BlockPos pos, Direction dir, boolean retracted, CallbackInfo ci) {
if (world.isClient) {
return;
}
var claims = ClaimUtils.getClaimsAt(world, pos);
this.claimsEmpty = claims.isEmpty();
this.trusted = new HashSet<>();
Expand All @@ -41,6 +44,9 @@ private void storeClaimInfo(World world, BlockPos pos, Direction dir, boolean re

@ModifyReturnValue(method = "calculatePush", at = @At("RETURN"))
private boolean preventMovement(boolean value) {
if (world.isClient) {
return value;
}
if (value) {
if (!checkClaims(this.movedBlocks) || !checkClaims(this.brokenBlocks)) {
this.movedBlocks.clear();
Expand All @@ -53,6 +59,7 @@ private boolean preventMovement(boolean value) {
return false;
}

@Unique
private boolean checkClaims(List<BlockPos> blocks) {
for (var pos : blocks) {
var claims = ClaimUtils.getClaimsAt(this.world, pos);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/draylar/goml/mixin/ProjectileEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public ProjectileEntityMixin(EntityType<?> type, World world) {

@Inject(method = "onCollision", at = @At("HEAD"), cancellable = true)
private void preventEffects(HitResult hitResult, CallbackInfo ci) {
if (getWorld().isClient) {
return;
}
if (!ClaimUtils.hasMatchingClaims(this.getWorld(), this.getBlockPos(), this.goml$getOriginSafe(), this.ownerUuid)) {
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public SmallFireballEntityMixin(EntityType<? extends AbstractFireballEntity> ent

@WrapWithCondition(method = "onBlockHit", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)Z"))
private boolean safeSetBlock(World world, BlockPos pos, BlockState state) {
if (world.isClient) {
return true;
}
return ClaimUtils.canExplosionDestroy(world, pos, this);
}
}
3 changes: 3 additions & 0 deletions src/main/java/draylar/goml/mixin/TntEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public TntEntityMixin(EntityType<?> type, World world) {

@Inject(at = @At("HEAD"), method = "explode", cancellable = true)
private void goml_attemptExplosion(CallbackInfo ci) {
if (getWorld().isClient) {
return;
}
if (causingEntity instanceof PlayerEntity) {
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(getWorld(), getBlockPos());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"i": "minecraft:nether_star",
"I": "minecraft:soul_sand",
"O": "goml:emeradic_claim_anchor"
}
},
"result": {
"id": "goml:withered_claim_anchor",
Expand Down

0 comments on commit 9111dd0

Please sign in to comment.