Skip to content

Commit

Permalink
change || to && for permission check to fix claims, refactor event ha…
Browse files Browse the repository at this point in the history
…ndlers, 1.2.1 beta
  • Loading branch information
Draylar committed Sep 27, 2020
1 parent 20a94b1 commit b03d20c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 59 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.16.2+build.47
loader_version=0.9.3+build.207

# Mod Properties
mod_version = 1.2.0-beta
mod_version = 1.2.1-beta
maven_group = com.github.draylar
archives_base_name = get-off-my-lawn

Expand Down
84 changes: 26 additions & 58 deletions src/main/java/draylar/goml/EventHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;

public class EventHandlers {

public static final TranslatableText BLOCK_PROTECTED = new TranslatableText("goml.block_protected");
public static final TranslatableText ENTITY_PROTECTED = new TranslatableText("goml.entity_protected");
public static final TranslatableText AREA_PROTECTED = new TranslatableText("goml.area_protected");

private EventHandlers() {
// NO-OP
}
Expand All @@ -28,84 +33,47 @@ public static void init() {
private static void registerInteractEntityCallback() {
UseEntityCallback.EVENT.register((playerEntity, world, hand, entity, entityHitResult) -> {
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(world, entity.getBlockPos());

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

if(noPermission || !playerEntity.hasPermissionLevel(3)) {
playerEntity.sendMessage(new TranslatableText("goml.entity_protected"), true);
return ActionResult.FAIL;
}
}

return ActionResult.PASS;
return testPermission(claimsFound, playerEntity, ENTITY_PROTECTED);
});
}

private static void registerAttackEntityCallback() {
AttackEntityCallback.EVENT.register(((playerEntity, world, hand, entity, entityHitResult) -> {
AttackEntityCallback.EVENT.register((playerEntity, world, hand, entity, entityHitResult) -> {
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(world, entity.getBlockPos());

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

if(noPermission || !playerEntity.hasPermissionLevel(3)) {
playerEntity.sendMessage(new TranslatableText("goml.entity_protected"), true);
return ActionResult.FAIL;
}
}

return ActionResult.PASS;
}));
return testPermission(claimsFound, playerEntity, ENTITY_PROTECTED);
});
}

private static void registerInteractBlockCallback() {
UseBlockCallback.EVENT.register(((playerEntity, world, hand, blockHitResult) -> {
UseBlockCallback.EVENT.register((playerEntity, world, hand, blockHitResult) -> {
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(world, blockHitResult.getBlockPos());

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

if(noPermission || !playerEntity.hasPermissionLevel(3)) {
playerEntity.sendMessage(new TranslatableText("goml.block_protected"), true);
return ActionResult.FAIL;
}
}

return ActionResult.PASS;
}));
return testPermission(claimsFound, playerEntity, AREA_PROTECTED);
});

// handle placing blocks at side of block not in claim
UseBlockCallback.EVENT.register(((playerEntity, world, hand, blockHitResult) -> {
UseBlockCallback.EVENT.register((playerEntity, world, hand, blockHitResult) -> {
Selection<Entry<ClaimBox, Claim>> claimsFound = ClaimUtils.getClaimsAt(world, blockHitResult.getBlockPos().offset(blockHitResult.getSide()));

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

if(noPermission || !playerEntity.hasPermissionLevel(3)) {
playerEntity.sendMessage(new TranslatableText("goml.block_protected"), true);
return ActionResult.FAIL;
}
}

return ActionResult.PASS;
}));
return testPermission(claimsFound, playerEntity, AREA_PROTECTED);
});
}

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

if(!claimsFound.isEmpty()) {
boolean noPermission = claimsFound.anyMatch((Entry<ClaimBox, Claim> boxInfo) -> !boxInfo.getValue().hasPermission(playerEntity));
private static ActionResult testPermission(Selection<Entry<ClaimBox, Claim>> claims, PlayerEntity player, TranslatableText error) {
if(!claims.isEmpty()) {
boolean noPermission = claims.anyMatch((Entry<ClaimBox, Claim> boxInfo) -> !boxInfo.getValue().hasPermission(player));

if(noPermission || !playerEntity.hasPermissionLevel(3)) {
playerEntity.sendMessage(new TranslatableText("goml.block_protected"), true);
return ActionResult.FAIL;
}
if(noPermission && !player.hasPermissionLevel(3)) {
player.sendMessage(error, true);
return ActionResult.FAIL;
}
}

return ActionResult.PASS;
});
return ActionResult.PASS;
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/goml/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"goml.untrusted": "%s has been removed as a trusted member from this claim.",
"goml.block_protected": "This block is protected by a claim!",
"goml.entity_protected": "This entity is protected by a claim!",
"goml.area_protected": "This area is protected by a claim!",
"goml.add_self": "You are already allowed in this claim!",
"goml.remove_self": "You can't remove yourself from this claim!",
"goml.removed_claim": "Removed claim in %s with origin of %s.",
Expand Down

0 comments on commit b03d20c

Please sign in to comment.