-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProtectionChecker.java
More file actions
1871 lines (1681 loc) · 71 KB
/
Copy pathProtectionChecker.java
File metadata and controls
1871 lines (1681 loc) · 71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.hyperfactions.protection;
import com.hyperfactions.HyperFactions;
import com.hyperfactions.config.ConfigManager;
import com.hyperfactions.data.Faction;
import com.hyperfactions.data.FactionMember;
import com.hyperfactions.data.FactionPermissions;
import com.hyperfactions.data.FactionRole;
import com.hyperfactions.data.RelationType;
import com.hyperfactions.data.Zone;
import com.hyperfactions.data.ZoneFlags;
import com.hyperfactions.integration.PermissionManager;
import com.hyperfactions.integration.protection.GravestoneIntegration;
import com.hyperfactions.integration.protection.OrbisMixinsIntegration;
import com.hyperfactions.manager.*;
import com.hyperfactions.util.ChunkUtil;
import com.hyperfactions.util.ErrorHandler;
import com.hyperfactions.util.HFMessages;
import com.hyperfactions.util.Logger;
import com.hyperfactions.util.CommonKeys;
import java.util.UUID;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Central class for all protection checks.
*/
public class ProtectionChecker {
private final Supplier<HyperFactions> plugin;
private final FactionManager factionManager;
private final ClaimManager claimManager;
private final ZoneManager zoneManager;
private final RelationManager relationManager;
private final CombatTagManager combatTagManager;
private GravestoneIntegration gravestoneIntegration;
/** Creates a new ProtectionChecker. */
public ProtectionChecker(
@NotNull FactionManager factionManager,
@NotNull ClaimManager claimManager,
@NotNull ZoneManager zoneManager,
@NotNull RelationManager relationManager,
@NotNull CombatTagManager combatTagManager
) {
this(null, factionManager, claimManager, zoneManager, relationManager, combatTagManager);
}
/** Creates a new ProtectionChecker. */
public ProtectionChecker(
@Nullable Supplier<HyperFactions> plugin,
@NotNull FactionManager factionManager,
@NotNull ClaimManager claimManager,
@NotNull ZoneManager zoneManager,
@NotNull RelationManager relationManager,
@NotNull CombatTagManager combatTagManager
) {
this.plugin = plugin;
this.factionManager = factionManager;
this.claimManager = claimManager;
this.zoneManager = zoneManager;
this.relationManager = relationManager;
this.combatTagManager = combatTagManager;
}
/**
* Result of a protection check for interactions.
*/
public enum ProtectionResult {
ALLOWED,
ALLOWED_BYPASS,
ALLOWED_WILDERNESS,
ALLOWED_SAFEZONE,
ALLOWED_OWN_CLAIM,
ALLOWED_ALLY_CLAIM,
ALLOWED_WARZONE,
DENIED_SAFEZONE,
DENIED_WARZONE,
DENIED_ENEMY_CLAIM,
DENIED_NEUTRAL_CLAIM,
DENIED_NO_PERMISSION
}
/**
* Result of a PvP check.
*/
public enum PvPResult {
ALLOWED,
ALLOWED_WARZONE,
DENIED_SAFEZONE,
DENIED_SAME_FACTION,
DENIED_ALLY,
DENIED_ATTACKER_SAFEZONE,
DENIED_DEFENDER_SAFEZONE,
DENIED_SPAWN_PROTECTED,
DENIED_TERRITORY_NO_PVP
}
/**
* Types of interactions to check.
*/
public enum InteractionType {
BUILD, // Place/break blocks
INTERACT, // General block interaction (fallback)
CONTAINER, // Open chests, etc.
DOOR, // Use doors/gates
BENCH, // Crafting tables
PROCESSING, // Furnaces/smelters
SEAT, // Seats/mounts
LIGHT, // Lights/lanterns/campfires
DAMAGE, // Damage entities (not players)
USE, // Use items (fallback)
TELEPORTER, // Use teleporter blocks
PORTAL, // Use portal blocks
CRATE_PICKUP, // Capture crate entity pickup
CRATE_PLACE, // Capture crate entity release
NPC_TAME, // F-key NPC taming
NPC_INTERACT, // NPC shops/dialogue interaction
MOUNT, // Mount/ride entities
PVE_DAMAGE, // Damage non-player entities (mobs)
ITEM_DROP, // Drop items
ITEM_PICKUP // Pick up items
}
// === Interaction Protection ===
/**
* Checks if a player can interact at a location.
*
* @param playerUuid the player's UUID
* @param world the world name
* @param x the world X coordinate
* @param z the world Z coordinate
* @param type the interaction type
* @return the protection result
*/
@NotNull
public ProtectionResult canInteract(@NotNull UUID playerUuid, @NotNull String world,
double x, double z, @NotNull InteractionType type) {
int chunkX = ChunkUtil.toChunkCoord(x);
int chunkZ = ChunkUtil.toChunkCoord(z);
return canInteractChunk(playerUuid, world, chunkX, chunkZ, type);
}
/**
* Checks if a player can interact in a chunk.
* Fail-closed: returns DENIED on any exception.
*
* @param playerUuid the player's UUID
* @param world the world name
* @param chunkX the chunk X coordinate
* @param chunkZ the chunk Z coordinate
* @param type the interaction type
* @return the protection result
*/
@NotNull
public ProtectionResult canInteractChunk(@NotNull UUID playerUuid, @NotNull String world,
int chunkX, int chunkZ, @NotNull InteractionType type) {
try {
// 1. Check if player is an admin (has admin.use permission)
boolean isAdmin = PermissionManager.get().hasPermission(playerUuid, "hyperfactions.admin.use");
// 2. Admin bypass check - admins ONLY bypass via toggle (not standard bypass perms)
if (isAdmin) {
if (plugin != null) {
HyperFactions hyperFactions = plugin.get();
if (hyperFactions != null && hyperFactions.isAdminBypassEnabled(playerUuid)) {
return ProtectionResult.ALLOWED_BYPASS;
}
}
// Admin with bypass OFF - continue to normal protection checks (no standard bypass)
} else {
// 3. Non-admin: Check standard bypass permissions
String bypassPerm = switch (type) {
case BUILD -> "hyperfactions.bypass.build";
case INTERACT, DOOR, BENCH, PROCESSING, SEAT, LIGHT, MOUNT, TELEPORTER, PORTAL,
CRATE_PICKUP, CRATE_PLACE, NPC_TAME, NPC_INTERACT,
ITEM_DROP, ITEM_PICKUP -> "hyperfactions.bypass.interact";
case CONTAINER -> "hyperfactions.bypass.container";
case DAMAGE, PVE_DAMAGE -> "hyperfactions.bypass.damage";
case USE -> "hyperfactions.bypass.use";
};
if (PermissionManager.get().hasPermission(playerUuid, bypassPerm)
|| PermissionManager.get().hasPermission(playerUuid, "hyperfactions.bypass.*")) {
return ProtectionResult.ALLOWED_BYPASS;
}
}
// 2. Check zone
Zone zone = zoneManager.getZone(world, chunkX, chunkZ);
if (zone != null) {
// Get the appropriate flag for the interaction type
String flagName = switch (type) {
case BUILD -> ZoneFlags.BUILD_ALLOWED;
case INTERACT, USE -> ZoneFlags.BLOCK_INTERACT;
case DOOR -> ZoneFlags.DOOR_USE;
case CONTAINER -> ZoneFlags.CONTAINER_USE;
case BENCH -> ZoneFlags.BENCH_USE;
case PROCESSING -> ZoneFlags.PROCESSING_USE;
case SEAT -> ZoneFlags.SEAT_USE;
case LIGHT -> ZoneFlags.LIGHT_USE;
case TELEPORTER -> ZoneFlags.TELEPORTER_USE;
case PORTAL -> ZoneFlags.PORTAL_USE;
case DAMAGE -> ZoneFlags.PVP_ENABLED;
case PVE_DAMAGE -> ZoneFlags.PVE_DAMAGE;
case CRATE_PICKUP -> ZoneFlags.CRATE_PICKUP;
case CRATE_PLACE -> ZoneFlags.CRATE_PLACE;
case NPC_TAME -> ZoneFlags.NPC_TAME;
case NPC_INTERACT -> ZoneFlags.NPC_INTERACT;
case MOUNT -> ZoneFlags.MOUNT_USE;
case ITEM_DROP, ITEM_PICKUP -> ZoneFlags.BLOCK_INTERACT; // zone checks handled by ECS systems
};
boolean allowed = zone.getEffectiveFlag(flagName);
// Debug: Log zone protection check
Logger.debug("[Protection] Zone '%s' (%s) flag '%s' = %s for player %s at %s/%d/%d",
zone.name(), zone.type().name(), flagName, allowed, playerUuid, world, chunkX, chunkZ);
if (!allowed) {
ProtectionResult result = zone.isSafeZone() ? ProtectionResult.DENIED_SAFEZONE
: zone.isWarZone() ? ProtectionResult.DENIED_WARZONE
: ProtectionResult.DENIED_NO_PERMISSION;
Logger.debug("[Protection] Zone blocked: %s", result);
return result;
}
// If zone allows this interaction, still need to check claim ownership below
// For WarZones with build allowed, anyone can interact
if (zone.isWarZone() && allowed) {
Logger.debug("[Protection] WarZone allowed: %s", ProtectionResult.ALLOWED_WARZONE);
return ProtectionResult.ALLOWED_WARZONE;
}
}
// Track whether we came from a zone for the wilderness result
boolean inSafeZone = zone != null && zone.isSafeZone();
// 3. Check claim owner
UUID claimOwner = claimManager.getClaimOwner(world, chunkX, chunkZ);
if (claimOwner == null) {
// No faction claim — return zone-aware result
return inSafeZone ? ProtectionResult.ALLOWED_SAFEZONE : ProtectionResult.ALLOWED_WILDERNESS;
}
// 4. Get player's faction
UUID playerFactionId = factionManager.getPlayerFactionId(playerUuid);
// 5. Get faction and its effective permissions
Faction ownerFaction = factionManager.getFaction(claimOwner);
FactionPermissions perms = null;
if (ownerFaction != null) {
perms = ConfigManager.get().getEffectiveFactionPermissions(
ownerFaction.getEffectivePermissions()
);
}
// 6. Check if same faction (member or officer)
if (playerFactionId != null && playerFactionId.equals(claimOwner)) {
// Determine if officer/leader or regular member
FactionMember factionMember = ownerFaction != null ? ownerFaction.getMember(playerUuid) : null;
boolean isOfficerOrLeader = factionMember != null
&& factionMember.role().getLevel() >= FactionRole.OFFICER.getLevel();
if (isOfficerOrLeader) {
if (perms != null && !checkPermission(perms, "officer", type)) {
Logger.debugProtection("Interaction denied: player=%s, chunk=%s/%d/%d, type=%s, result=OFFICER_NO_PERM, claimOwner=%s",
playerUuid, world, chunkX, chunkZ, type, claimOwner);
return ProtectionResult.DENIED_NO_PERMISSION;
}
} else {
if (perms != null && !checkMemberPermission(perms, type)) {
Logger.debugProtection("Interaction denied: player=%s, chunk=%s/%d/%d, type=%s, result=MEMBER_NO_PERM, claimOwner=%s",
playerUuid, world, chunkX, chunkZ, type, claimOwner);
return ProtectionResult.DENIED_NO_PERMISSION;
}
}
return ProtectionResult.ALLOWED_OWN_CLAIM;
}
// 7. Check ally relation
if (playerFactionId != null) {
RelationType relation = relationManager.getRelation(playerFactionId, claimOwner);
if (relation == RelationType.ALLY) {
// Check ally permissions
if (perms != null && checkAllyPermission(perms, type)) {
return ProtectionResult.ALLOWED_ALLY_CLAIM;
}
// Ally but no permission for this type
Logger.debugProtection("Interaction denied: player=%s, chunk=%s/%d/%d, type=%s, result=ALLY_NO_PERM, claimOwner=%s",
playerUuid, world, chunkX, chunkZ, type, claimOwner);
return ProtectionResult.DENIED_NO_PERMISSION;
}
}
// 8. Check outsider permissions (neutral, enemy, or no faction)
if (perms != null && checkOutsiderPermission(perms, type)) {
return ProtectionResult.ALLOWED;
}
// 9. Denied - either enemy or neutral claim without permission
if (playerFactionId != null) {
RelationType relation = relationManager.getRelation(playerFactionId, claimOwner);
if (relation == RelationType.ENEMY) {
Logger.debugProtection("Interaction denied: player=%s, chunk=%s/%d/%d, type=%s, result=ENEMY_CLAIM, claimOwner=%s",
playerUuid, world, chunkX, chunkZ, type, claimOwner);
return ProtectionResult.DENIED_ENEMY_CLAIM;
}
}
Logger.debugProtection("Interaction denied: player=%s, chunk=%s/%d/%d, type=%s, result=NEUTRAL_CLAIM, claimOwner=%s",
playerUuid, world, chunkX, chunkZ, type, claimOwner);
return ProtectionResult.DENIED_NEUTRAL_CLAIM;
} catch (Exception e) {
// Fail-closed: deny on any exception to prevent unauthorized actions
ErrorHandler.report(String.format("Protection check error (fail-closed) for player %s at %s/%d/%d type=%s",
playerUuid, world, chunkX, chunkZ, type), e);
return ProtectionResult.DENIED_NO_PERMISSION;
}
}
/**
* Unified permission check for any level and interaction type.
* Uses parent-child logic built into FactionPermissions.get().
*
* @param perms the faction permissions
* @param level the level (outsider, ally, member, officer)
* @param type the interaction type
* @return true if allowed
*/
private boolean checkPermission(FactionPermissions perms, String level, InteractionType type) {
return switch (type) {
case BUILD -> perms.get(level + "Break") || perms.get(level + "Place");
case INTERACT, USE -> perms.get(level + "Interact");
case DOOR -> perms.get(level + "DoorUse");
case CONTAINER -> perms.get(level + "ContainerUse");
case BENCH -> perms.get(level + "BenchUse");
case PROCESSING -> perms.get(level + "ProcessingUse");
case SEAT -> perms.get(level + "SeatUse");
case LIGHT -> perms.get(level + "Interact"); // Light use shares general interact permission
case TELEPORTER, PORTAL -> perms.get(level + "TransportUse");
case CRATE_PICKUP, CRATE_PLACE -> perms.get(level + "CrateUse");
case NPC_TAME -> perms.get(level + "NpcTame");
case NPC_INTERACT -> perms.get(level + "NpcInteract");
case MOUNT -> perms.get(level + "SeatUse"); // Mount shares seat permission
case PVE_DAMAGE -> perms.get(level + "PveDamage");
case DAMAGE -> !"outsider".equals(level); // outsiders can't damage (PvP handled separately)
case ITEM_DROP, ITEM_PICKUP -> perms.get(level + "Interact"); // item drop/pickup uses interact permission
};
}
private boolean checkOutsiderPermission(FactionPermissions perms, InteractionType type) {
return checkPermission(perms, "outsider", type);
}
private boolean checkAllyPermission(FactionPermissions perms, InteractionType type) {
return checkPermission(perms, "ally", type);
}
private boolean checkMemberPermission(FactionPermissions perms, InteractionType type) {
return checkPermission(perms, "member", type);
}
// === PvP Protection ===
/**
* Checks if a player can damage another player.
*
* @param attackerUuid the attacker's UUID
* @param defenderUuid the defender's UUID
* @param world the world name
* @param x the location X
* @param z the location Z
* @return the PvP result
*/
@NotNull
public PvPResult canDamagePlayer(@NotNull UUID attackerUuid, @NotNull UUID defenderUuid,
@NotNull String world, double x, double z) {
int chunkX = ChunkUtil.toChunkCoord(x);
int chunkZ = ChunkUtil.toChunkCoord(z);
return canDamagePlayerChunk(attackerUuid, defenderUuid, world, chunkX, chunkZ);
}
/**
* Checks if a player can damage another player in a chunk.
*
* @param attackerUuid the attacker's UUID
* @param defenderUuid the defender's UUID
* @param world the world name
* @param chunkX the chunk X
* @param chunkZ the chunk Z
* @return the PvP result
*/
@NotNull
public PvPResult canDamagePlayerChunk(@NotNull UUID attackerUuid, @NotNull UUID defenderUuid,
@NotNull String world, int chunkX, int chunkZ) {
ConfigManager config = ConfigManager.get();
// 0. Check defender's spawn protection
if (combatTagManager.hasSpawnProtection(defenderUuid)) {
return PvPResult.DENIED_SPAWN_PROTECTED;
}
// 0b. Break attacker's spawn protection if they attack (if configured)
if (config.isSpawnProtectionBreakOnAttack() && combatTagManager.hasSpawnProtection(attackerUuid)) {
combatTagManager.clearSpawnProtection(attackerUuid);
}
// 1. Check zone for PvP flag
Zone zone = zoneManager.getZone(world, chunkX, chunkZ);
if (zone != null) {
boolean pvpEnabled = zone.getEffectiveFlag(ZoneFlags.PVP_ENABLED);
if (!pvpEnabled) {
return PvPResult.DENIED_SAFEZONE;
}
// Zone has PvP enabled - check friendly fire hierarchy
boolean friendlyFireAllowed = zone.getEffectiveFlag(ZoneFlags.FRIENDLY_FIRE);
// Check same faction (zone flag → child flag → config fallback)
if (factionManager.areInSameFaction(attackerUuid, defenderUuid)) {
if (friendlyFireAllowed) {
// Parent on — check granular faction child flag
if (!zone.getEffectiveFlag(ZoneFlags.FRIENDLY_FIRE_FACTION)) {
return PvPResult.DENIED_SAME_FACTION;
}
} else if (!config.isFactionDamage()) {
// Parent off and config doesn't allow — deny
return PvPResult.DENIED_SAME_FACTION;
}
}
// Check ally (zone flag → child flag → config fallback)
RelationType relation = relationManager.getPlayerRelation(attackerUuid, defenderUuid);
if (relation == RelationType.ALLY) {
if (friendlyFireAllowed) {
// Parent on — check granular ally child flag
if (!zone.getEffectiveFlag(ZoneFlags.FRIENDLY_FIRE_ALLY)) {
return PvPResult.DENIED_ALLY;
}
} else if (!config.isAllyDamage()) {
// Parent off and config doesn't allow — deny
return PvPResult.DENIED_ALLY;
}
}
// PvP is enabled in this zone
return zone.isWarZone() ? PvPResult.ALLOWED_WARZONE : PvPResult.ALLOWED;
}
// Not in a zone - use standard checks
// 2. Check faction territory PvP setting
UUID claimOwner = claimManager.getClaimOwner(world, chunkX, chunkZ);
if (claimOwner != null) {
Faction ownerFaction = factionManager.getFaction(claimOwner);
if (ownerFaction != null) {
FactionPermissions perms = config.getEffectiveFactionPermissions(
ownerFaction.getEffectivePermissions()
);
if (!perms.pvpEnabled()) {
Logger.debugProtection("PvP denied: attacker=%s, defender=%s, chunk=%s/%d/%d, result=TERRITORY_NO_PVP, claimOwner=%s",
attackerUuid, defenderUuid, world, chunkX, chunkZ, claimOwner);
return PvPResult.DENIED_TERRITORY_NO_PVP;
}
}
}
// 3. Check same faction (with per-world override)
if (factionManager.areInSameFaction(attackerUuid, defenderUuid)) {
if (!config.isFactionDamage(world)) {
return PvPResult.DENIED_SAME_FACTION;
}
}
// 4. Check ally (with per-world override)
RelationType relation = relationManager.getPlayerRelation(attackerUuid, defenderUuid);
if (relation == RelationType.ALLY) {
if (!config.isAllyDamage(world)) {
Logger.debugProtection("PvP denied: attacker=%s, defender=%s, chunk=%s/%d/%d, result=ALLY",
attackerUuid, defenderUuid, world, chunkX, chunkZ);
return PvPResult.DENIED_ALLY;
}
}
// 5. Check outsider damage config in claimed territory
if (claimOwner != null) {
UUID attackerFactionId = factionManager.getPlayerFactionId(attackerUuid);
if (attackerFactionId == null || !attackerFactionId.equals(claimOwner)) {
// Attacker is not the claim owner — check outsider damage config
if (attackerFactionId == null) {
// Factionless attacker
if (!config.isFactionlessDamageAllowed()) {
return PvPResult.DENIED_TERRITORY_NO_PVP;
}
} else if (relation == RelationType.ENEMY) {
if (!config.isEnemyDamageAllowed()) {
return PvPResult.DENIED_TERRITORY_NO_PVP;
}
} else if (relation == RelationType.NEUTRAL) {
if (!config.isNeutralDamageAllowed()) {
return PvPResult.DENIED_TERRITORY_NO_PVP;
}
}
}
}
// 6. Default: allow PvP
Logger.debugProtection("PvP allowed: attacker=%s, defender=%s, chunk=%s/%d/%d, relation=%s",
attackerUuid, defenderUuid, world, chunkX, chunkZ, relation);
return PvPResult.ALLOWED;
}
// === Convenience Methods ===
/**
* Checks if a player can build at a location.
*
* @param playerUuid the player's UUID
* @param world the world name
* @param x the X coordinate
* @param z the Z coordinate
* @return true if allowed
*/
public boolean canBuild(@NotNull UUID playerUuid, @NotNull String world, double x, double z) {
ProtectionResult result = canInteract(playerUuid, world, x, z, InteractionType.BUILD);
return isAllowed(result);
}
/**
* Checks if a player can access containers at a location.
*
* @param playerUuid the player's UUID
* @param world the world name
* @param x the X coordinate
* @param z the Z coordinate
* @return true if allowed
*/
public boolean canAccessContainer(@NotNull UUID playerUuid, @NotNull String world, double x, double z) {
ProtectionResult result = canInteract(playerUuid, world, x, z, InteractionType.CONTAINER);
return isAllowed(result);
}
/**
* Checks if a player can pick up items at a location (auto pickup mode).
*
* <p>This is for native ECS events (InteractivelyPickupItemEvent) and always
* checks ITEM_PICKUP flag. For mode-aware pickup checks, use the overload
* that accepts a mode parameter.
*
* @param playerUuid the player's UUID
* @param worldName the world name
* @param x the X coordinate
* @param y the Y coordinate (unused, but included for API consistency)
* @param z the Z coordinate
* @return true if pickup is allowed
*/
public boolean canPickupItem(@NotNull UUID playerUuid, @NotNull String worldName, double x, double y, double z) {
return canPickupItem(playerUuid, worldName, x, y, z, "auto");
}
/**
* Checks if a player can pick up items at a location with pickup mode awareness.
*
* <p>This is called by OrbisGuard-Mixins hook for F-key and auto pickup events.
* It checks:
* 1. Admin bypass toggle
* 2. Bypass permission (hyperfactions.bypass.pickup)
* 3. Zone flags (ITEM_PICKUP for auto, ITEM_PICKUP_MANUAL for F-key)
* 4. Faction claim permissions
*
* @param playerUuid the player's UUID
* @param worldName the world name
* @param x the X coordinate
* @param y the Y coordinate (unused, but included for API consistency)
* @param z the Z coordinate
* @param mode the pickup mode: "auto" for walking over items, "manual" for F-key
* @return true if pickup is allowed
*/
public boolean canPickupItem(@NotNull UUID playerUuid, @NotNull String worldName, double x, double y, double z, @NotNull String mode) {
int chunkX = ChunkUtil.toChunkCoord(x);
int chunkZ = ChunkUtil.toChunkCoord(z);
// Determine which flag to check based on pickup mode
// "manual" = F-key pickup → ITEM_PICKUP_MANUAL
// "auto" or anything else = auto pickup → ITEM_PICKUP
boolean isManualPickup = "manual".equalsIgnoreCase(mode);
String flagToCheck = isManualPickup ? ZoneFlags.ITEM_PICKUP_MANUAL : ZoneFlags.ITEM_PICKUP;
// 1. Check admin bypass toggle
if (plugin != null) {
HyperFactions hyperFactions = plugin.get();
if (hyperFactions != null && hyperFactions.isAdminBypassEnabled(playerUuid)) {
Logger.debug("[Pickup:%s] Admin bypass enabled for %s", mode, playerUuid);
return true;
}
}
// 2. Check bypass permission
if (PermissionManager.get().hasPermission(playerUuid, "hyperfactions.bypass.pickup")
|| PermissionManager.get().hasPermission(playerUuid, "hyperfactions.bypass.*")) {
Logger.debug("[Pickup:%s] Bypass permission for %s", mode, playerUuid);
return true;
}
// 3. Check zone flags (check appropriate flag based on mode)
Zone zone = zoneManager.getZone(worldName, chunkX, chunkZ);
if (zone != null) {
boolean pickupAllowed = zone.getEffectiveFlag(flagToCheck);
if (!pickupAllowed) {
Logger.debug("[Pickup:%s] Blocked by zone '%s' flag '%s'=false for %s at %s/%d/%d",
mode, zone.name(), flagToCheck, playerUuid, worldName, chunkX, chunkZ);
return false;
}
}
// 4. Check faction claim
UUID claimOwner = claimManager.getClaimOwner(worldName, chunkX, chunkZ);
if (claimOwner == null) {
// Wilderness - pickup allowed
return true;
}
// 5. Get player's faction
UUID playerFactionId = factionManager.getPlayerFactionId(playerUuid);
// 6. Check if same faction (members can always pick up in own territory)
if (playerFactionId != null && playerFactionId.equals(claimOwner)) {
return true;
}
// 7. Check ally relation (allies can pick up in allied territory)
if (playerFactionId != null) {
RelationType relation = relationManager.getRelation(playerFactionId, claimOwner);
if (relation == RelationType.ALLY) {
return true;
}
}
// 8. Outsider pickup — configurable via server settings
if (!ConfigManager.get().isOutsiderPickupAllowed()) {
Logger.debug("[Pickup:%s] Blocked in other faction's territory for %s at %s/%d/%d",
mode, playerUuid, worldName, chunkX, chunkZ);
return false;
}
return true;
}
/**
* Checks if a protection result is "allowed".
*
* @param result the result
* @return true if allowed
*/
public boolean isAllowed(@NotNull ProtectionResult result) {
return switch (result) {
case ALLOWED, ALLOWED_BYPASS, ALLOWED_WILDERNESS, ALLOWED_SAFEZONE,
ALLOWED_OWN_CLAIM, ALLOWED_ALLY_CLAIM, ALLOWED_WARZONE -> true;
default -> false;
};
}
/**
* Checks if a PvP result is "allowed".
*
* @param result the result
* @return true if allowed
*/
public boolean isAllowed(@NotNull PvPResult result) {
return switch (result) {
case ALLOWED, ALLOWED_WARZONE -> true;
default -> false;
};
}
/**
* Looks up a PlayerRef from a UUID for i18n message resolution.
* Returns null if the player is offline or plugin is unavailable.
*/
@Nullable
private PlayerRef lookupPlayerRef(@Nullable UUID uuid) {
if (uuid == null || plugin == null) {
return null;
}
HyperFactions hf = plugin.get();
return hf != null ? hf.lookupPlayer(uuid) : null;
}
/**
* Gets a user-friendly denial message with generic action wording (server default language).
*/
@NotNull
public String getDenialMessage(@NotNull ProtectionResult result) {
return getDenialMessage(null, result, null);
}
/**
* Gets a user-friendly denial message with specific action context (server default language).
*/
@NotNull
public String getDenialMessage(@NotNull ProtectionResult result, @Nullable InteractionType type) {
return getDenialMessage(null, result, type);
}
/**
* Gets a user-friendly denial message localized to the player's language.
*
* @param player the player (null for server default language)
* @param result the protection result
* @param type the interaction type (null for generic messages)
* @return the denial message
*/
@NotNull
public String getDenialMessage(@Nullable PlayerRef player, @NotNull ProtectionResult result,
@Nullable InteractionType type) {
String action = getActionPhrase(player, type);
return switch (result) {
case DENIED_SAFEZONE -> HFMessages.get(player, CommonKeys.Protection.DENIED_SAFEZONE, action);
case DENIED_WARZONE -> HFMessages.get(player, CommonKeys.Protection.DENIED_WARZONE, action);
case DENIED_ENEMY_CLAIM -> HFMessages.get(player, CommonKeys.Protection.DENIED_ENEMY_CLAIM, action);
case DENIED_NEUTRAL_CLAIM -> HFMessages.get(player, CommonKeys.Protection.DENIED_CLAIMED, action);
case DENIED_NO_PERMISSION -> HFMessages.get(player, CommonKeys.Protection.DENIED_HERE, action);
default -> HFMessages.get(player, CommonKeys.Protection.DENIED_HERE, action);
};
}
/**
* Gets a player-friendly action phrase for the given interaction type.
*
* @param player the player (null for server default language)
* @param type the interaction type, or null for generic
* @return phrase like "You can't build or break blocks"
*/
@NotNull
private String getActionPhrase(@Nullable PlayerRef player, @Nullable InteractionType type) {
if (type == null) {
return HFMessages.get(player, CommonKeys.Protection.ACTION_GENERIC);
}
return switch (type) {
case BUILD -> HFMessages.get(player, CommonKeys.Protection.ACTION_BUILD);
case INTERACT, USE -> HFMessages.get(player, CommonKeys.Protection.ACTION_INTERACT);
case DOOR -> HFMessages.get(player, CommonKeys.Protection.ACTION_DOOR);
case CONTAINER -> HFMessages.get(player, CommonKeys.Protection.ACTION_CONTAINER);
case BENCH -> HFMessages.get(player, CommonKeys.Protection.ACTION_BENCH);
case PROCESSING -> HFMessages.get(player, CommonKeys.Protection.ACTION_PROCESSING);
case SEAT -> HFMessages.get(player, CommonKeys.Protection.ACTION_SEAT);
case LIGHT -> HFMessages.get(player, CommonKeys.Protection.ACTION_LIGHT);
case TELEPORTER, PORTAL -> HFMessages.get(player, CommonKeys.Protection.ACTION_TELEPORTER);
case CRATE_PICKUP, CRATE_PLACE -> HFMessages.get(player, CommonKeys.Protection.ACTION_CRATE);
case NPC_TAME -> HFMessages.get(player, CommonKeys.Protection.ACTION_TAME);
case NPC_INTERACT -> HFMessages.get(player, CommonKeys.Protection.ACTION_NPC);
case MOUNT -> HFMessages.get(player, CommonKeys.Protection.ACTION_MOUNT);
case PVE_DAMAGE -> HFMessages.get(player, CommonKeys.Protection.ACTION_PVE);
case DAMAGE -> HFMessages.get(player, CommonKeys.Protection.ACTION_GENERIC);
case ITEM_DROP -> HFMessages.get(player, CommonKeys.Protection.ACTION_ITEM_DROP);
case ITEM_PICKUP -> HFMessages.get(player, CommonKeys.Protection.ACTION_ITEM_PICKUP);
};
}
/**
* Gets a user-friendly PvP denial message (server default language).
*/
@NotNull
public String getDenialMessage(@NotNull PvPResult result) {
return getDenialMessage(null, result);
}
/**
* Gets a user-friendly PvP denial message localized to the player's language.
*
* @param player the player (null for server default language)
* @param result the PvP result
* @return the denial message
*/
@NotNull
public String getDenialMessage(@Nullable PlayerRef player, @NotNull PvPResult result) {
return switch (result) {
case DENIED_SAFEZONE -> HFMessages.get(player, CommonKeys.Protection.PVP_SAFEZONE);
case DENIED_SAME_FACTION -> HFMessages.get(player, CommonKeys.Protection.PVP_SAME_FACTION);
case DENIED_ALLY -> HFMessages.get(player, CommonKeys.Protection.PVP_ALLY);
case DENIED_ATTACKER_SAFEZONE, DENIED_DEFENDER_SAFEZONE -> HFMessages.get(player, CommonKeys.Protection.PVP_SAFEZONE);
case DENIED_SPAWN_PROTECTED -> HFMessages.get(player, CommonKeys.Protection.PVP_SPAWN_PROTECTED);
case DENIED_TERRITORY_NO_PVP -> HFMessages.get(player, CommonKeys.Protection.PVP_TERRITORY_DISABLED);
default -> HFMessages.get(player, CommonKeys.Protection.PVP_GENERIC);
};
}
// === Mixin Hook Protection Methods ===
// These methods are called by HyperProtect/OrbisGuard mixin hooks.
// They accept block coordinates (int x, y, z) and return a String denial
// message (null = allowed).
/**
* Common protection check for mixin hooks.
* Checks bypass, specific zone flag, and faction claim permissions.
* Fail-closed: returns a denial message on any exception.
*
* @param playerUuid the player's UUID
* @param worldName the world name
* @param x the block X coordinate
* @param y the block Y coordinate
* @param z the block Z coordinate
* @param zoneFlag the specific zone flag to check
* @param factionType the interaction type for faction permission resolution
* @return null if allowed, denial message if denied
*/
@Nullable
private String checkMixinProtection(@NotNull UUID playerUuid, @NotNull String worldName,
int x, int y, int z,
@NotNull String zoneFlag,
@NotNull InteractionType factionType) {
try {
int chunkX = ChunkUtil.toChunkCoord(x);
int chunkZ = ChunkUtil.toChunkCoord(z);
// 1. Admin bypass
if (plugin != null) {
HyperFactions hf = plugin.get();
if (hf != null && hf.isAdminBypassEnabled(playerUuid)) {
return null;
}
}
// 2. Standard bypass
boolean isAdmin = PermissionManager.get().hasPermission(playerUuid, "hyperfactions.admin.use");
if (!isAdmin) {
String bypassPerm = switch (factionType) {
case BUILD -> "hyperfactions.bypass.build";
case INTERACT, DOOR, BENCH, PROCESSING, SEAT, LIGHT, MOUNT, TELEPORTER, PORTAL,
CRATE_PICKUP, CRATE_PLACE, NPC_TAME, NPC_INTERACT,
ITEM_DROP, ITEM_PICKUP -> "hyperfactions.bypass.interact";
case CONTAINER -> "hyperfactions.bypass.container";
case DAMAGE, PVE_DAMAGE -> "hyperfactions.bypass.damage";
case USE -> "hyperfactions.bypass.use";
};
if (PermissionManager.get().hasPermission(playerUuid, bypassPerm)
|| PermissionManager.get().hasPermission(playerUuid, "hyperfactions.bypass.*")) {
return null;
}
}
// Resolve player's locale for localized denial messages
PlayerRef playerRef = lookupPlayerRef(playerUuid);
// 3. Zone flag check
Zone zone = zoneManager.getZone(worldName, chunkX, chunkZ);
if (zone != null) {
if (!zone.getEffectiveFlag(zoneFlag)) {
String action = getActionPhrase(playerRef, factionType);
if (zone.isSafeZone()) {
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_SAFEZONE, action);
}
if (zone.isWarZone()) {
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_WARZONE, action);
}
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_ZONE, action);
}
if (zone.isWarZone()) {
return null;
}
}
// 4. Faction claim check
UUID claimOwner = claimManager.getClaimOwner(worldName, chunkX, chunkZ);
if (claimOwner == null) { // Wilderness
return null;
}
UUID playerFactionId = factionManager.getPlayerFactionId(playerUuid);
Faction ownerFaction = factionManager.getFaction(claimOwner);
FactionPermissions perms = null;
if (ownerFaction != null) {
perms = ConfigManager.get().getEffectiveFactionPermissions(ownerFaction.getEffectivePermissions());
}
// Same faction
if (playerFactionId != null && playerFactionId.equals(claimOwner)) {
FactionMember member = ownerFaction != null ? ownerFaction.getMember(playerUuid) : null;
boolean isOfficerOrLeader = member != null
&& member.role().getLevel() >= FactionRole.OFFICER.getLevel();
String level = isOfficerOrLeader ? "officer" : "member";
if (perms != null && !checkPermission(perms, level, factionType)) {
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_FACTION_PERM, getActionPhrase(playerRef, factionType), level);
}
return null;
}
// Ally
if (playerFactionId != null) {
RelationType relation = relationManager.getRelation(playerFactionId, claimOwner);
if (relation == RelationType.ALLY) {
if (perms != null && checkPermission(perms, "ally", factionType)) {
return null;
}
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_ALLY_TERRITORY, getActionPhrase(playerRef, factionType));
}
}
// Outsider
if (perms != null && checkPermission(perms, "outsider", factionType)) {
return null;
}
// Determine territory context for the message
if (playerFactionId != null) {
RelationType relation = relationManager.getRelation(playerFactionId, claimOwner);
if (relation == RelationType.ENEMY) {
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_ENEMY_CLAIM, getActionPhrase(playerRef, factionType));
}
}
return HFMessages.get(playerRef, CommonKeys.Protection.DENIED_CLAIMED, getActionPhrase(playerRef, factionType));
} catch (Exception e) {
// Fail-closed: deny on any exception to prevent unauthorized actions
ErrorHandler.report(String.format("Protection check error (fail-closed) for player %s at %s/%d/%d/%d type=%s",
playerUuid, worldName, x, y, z, factionType), e);
return HFMessages.get(lookupPlayerRef(playerUuid), CommonKeys.Protection.DENIED_ERROR);
}
}
/**
* Checks if a player can break/build blocks (mixin hook version).
*
* @return null if allowed, denial message if denied
*/
@Nullable
public String checkBuild(@NotNull UUID playerUuid, @NotNull String worldName, int x, int y, int z) {
return checkMixinProtection(playerUuid, worldName, x, y, z, ZoneFlags.BUILD_ALLOWED, InteractionType.BUILD);
}
/**
* Checks if a player can place blocks (mixin hook version).
*
* @return null if allowed, denial message if denied
*/
@Nullable
public String checkPlace(@NotNull UUID playerUuid, @NotNull String worldName, int x, int y, int z) {
return checkMixinProtection(playerUuid, worldName, x, y, z, ZoneFlags.BLOCK_PLACE, InteractionType.BUILD);
}
/**
* Checks if a player can use the hammer (mixin hook version).
*
* @return null if allowed, denial message if denied
*/
@Nullable
public String checkHammer(@NotNull UUID playerUuid, @NotNull String worldName, int x, int y, int z) {
return checkMixinProtection(playerUuid, worldName, x, y, z, ZoneFlags.HAMMER_USE, InteractionType.BUILD);
}
/**
* Checks if a player can use builder tools (mixin hook version).
*
* @return null if allowed, denial message if denied
*/
@Nullable
public String checkBuilderTool(@NotNull UUID playerUuid, @NotNull String worldName, int x, int y, int z) {
return checkMixinProtection(playerUuid, worldName, x, y, z, ZoneFlags.BUILDER_TOOLS_USE, InteractionType.BUILD);
}
/**
* Checks if a player can interact with blocks (mixin hook version for ChangeState).
*
* @return null if allowed, denial message if denied
*/
@Nullable
public String checkUse(@NotNull UUID playerUuid, @NotNull String worldName, int x, int y, int z) {
return checkUse(playerUuid, worldName, x, y, z, InteractionType.INTERACT);
}
/**
* Checks if a player can use/interact at block coordinates with explicit interaction type.
* Routes to the correct zone flag and faction permission based on the interaction type.
*
* @param type the specific interaction type (CRATE_PICKUP, CRATE_PLACE, NPC_TAME, or INTERACT)
* @return null if allowed, denial message if denied
*/
@Nullable
public String checkUse(@NotNull UUID playerUuid, @NotNull String worldName, int x, int y, int z,
@NotNull InteractionType type) {
String zoneFlag = switch (type) {
case CRATE_PICKUP -> ZoneFlags.CRATE_PICKUP;
case CRATE_PLACE -> ZoneFlags.CRATE_PLACE;
case NPC_TAME -> ZoneFlags.NPC_TAME;
case MOUNT -> ZoneFlags.MOUNT_USE;
case LIGHT -> ZoneFlags.LIGHT_USE;
default -> ZoneFlags.BLOCK_INTERACT;
};
return checkMixinProtection(playerUuid, worldName, x, y, z, zoneFlag, type);