2021-06-08 21:03:16 +00:00
package io.github.flemmli97.flan.claim ;
2020-08-23 12:52:36 +00:00
2020-08-23 21:18:18 +00:00
import com.google.common.collect.ImmutableList ;
2021-05-02 16:24:46 +00:00
import com.google.common.collect.Lists ;
2020-08-23 12:52:36 +00:00
import com.google.gson.JsonArray ;
2020-08-24 19:03:06 +00:00
import com.google.gson.JsonElement ;
2020-08-23 12:52:36 +00:00
import com.google.gson.JsonObject ;
2022-01-08 13:53:13 +00:00
import com.mojang.authlib.GameProfile ;
2021-06-08 21:06:01 +00:00
import io.github.flemmli97.flan.Flan ;
2021-07-20 20:57:34 +00:00
import io.github.flemmli97.flan.api.data.IPermissionContainer ;
import io.github.flemmli97.flan.api.permission.ClaimPermission ;
import io.github.flemmli97.flan.api.permission.PermissionRegistry ;
2021-06-09 10:19:43 +00:00
import io.github.flemmli97.flan.config.Config ;
2021-06-08 21:06:01 +00:00
import io.github.flemmli97.flan.config.ConfigHandler ;
2022-02-18 18:11:51 +00:00
import io.github.flemmli97.flan.platform.ClaimPermissionCheck ;
import io.github.flemmli97.flan.platform.CrossPlatformStuff ;
2022-11-26 19:47:39 +00:00
import io.github.flemmli97.flan.platform.integration.webmap.WebmapCalls ;
2021-08-13 09:20:55 +00:00
import io.github.flemmli97.flan.player.LogoutTracker ;
2021-06-08 21:06:01 +00:00
import io.github.flemmli97.flan.player.PlayerClaimData ;
2022-12-18 19:37:36 +00:00
import io.github.flemmli97.flan.player.display.ClaimDisplayBox ;
import io.github.flemmli97.flan.player.display.DisplayBox ;
2021-12-03 21:37:35 +00:00
import net.minecraft.ChatFormatting ;
import net.minecraft.core.BlockPos ;
import net.minecraft.network.chat.Component ;
2022-07-01 13:19:50 +00:00
import net.minecraft.network.chat.MutableComponent ;
2022-11-26 19:47:39 +00:00
import net.minecraft.network.chat.Style ;
2021-12-03 21:37:35 +00:00
import net.minecraft.network.protocol.game.ClientboundSetSubtitleTextPacket ;
import net.minecraft.network.protocol.game.ClientboundSetTitleTextPacket ;
import net.minecraft.resources.ResourceLocation ;
2020-08-23 12:52:36 +00:00
import net.minecraft.server.MinecraftServer ;
2021-12-03 21:37:35 +00:00
import net.minecraft.server.level.ServerLevel ;
import net.minecraft.server.level.ServerPlayer ;
import net.minecraft.world.InteractionResult ;
import net.minecraft.world.effect.MobEffect ;
import net.minecraft.world.effect.MobEffectInstance ;
2022-07-27 16:14:39 +00:00
import net.minecraft.world.effect.MobEffects ;
2021-12-03 21:37:35 +00:00
import net.minecraft.world.level.chunk.ChunkStatus ;
import net.minecraft.world.level.levelgen.Heightmap ;
import net.minecraft.world.phys.AABB ;
2020-08-23 12:52:36 +00:00
2022-11-26 19:47:39 +00:00
import javax.annotation.Nullable ;
2021-05-02 16:06:56 +00:00
import java.util.ArrayList ;
2020-08-23 21:18:18 +00:00
import java.util.Arrays ;
2022-01-31 15:32:21 +00:00
import java.util.Collection ;
2021-05-02 16:06:56 +00:00
import java.util.HashMap ;
import java.util.HashSet ;
2020-08-23 12:52:36 +00:00
import java.util.List ;
import java.util.Map ;
2021-07-02 18:01:27 +00:00
import java.util.Optional ;
2020-08-24 20:32:29 +00:00
import java.util.Set ;
2020-08-23 12:52:36 +00:00
import java.util.UUID ;
2020-11-28 19:53:29 +00:00
public class Claim implements IPermissionContainer {
2020-08-23 12:52:36 +00:00
private boolean dirty ;
private int minX , minZ , maxX , maxZ , minY ;
private UUID owner ;
2020-08-23 21:18:18 +00:00
2020-08-23 12:52:36 +00:00
private UUID claimID ;
2021-06-10 15:29:30 +00:00
private String claimName = " " ;
2021-06-12 21:21:38 +00:00
private BlockPos homePos ;
2021-05-02 16:06:56 +00:00
private final Map < ClaimPermission , Boolean > globalPerm = new HashMap < > ( ) ;
private final Map < String , Map < ClaimPermission , Boolean > > permissions = new HashMap < > ( ) ;
2020-08-23 12:52:36 +00:00
2021-05-02 16:06:56 +00:00
private final Map < UUID , String > playersGroups = new HashMap < > ( ) ;
2020-08-23 12:52:36 +00:00
2023-03-11 17:17:18 +00:00
private final Set < UUID > fakePlayers = new HashSet < > ( ) ;
2021-05-02 16:06:56 +00:00
private final List < Claim > subClaims = new ArrayList < > ( ) ;
2020-08-23 12:52:36 +00:00
2020-08-24 19:03:06 +00:00
private UUID parent ;
private Claim parentClaim ;
2020-08-23 12:52:36 +00:00
/ * *
* Flag for players tracking this claim
* /
private boolean removed ;
2021-12-03 21:37:35 +00:00
private final ServerLevel world ;
2020-08-24 19:03:06 +00:00
2021-12-03 21:37:35 +00:00
private final Map < MobEffect , Integer > potions = new HashMap < > ( ) ;
2021-06-12 22:21:31 +00:00
2021-12-03 21:37:35 +00:00
public Component enterTitle , enterSubtitle , leaveTitle , leaveSubtitle ;
2021-09-14 15:26:41 +00:00
2021-12-03 21:37:35 +00:00
private Claim ( ServerLevel world ) {
2020-08-24 19:03:06 +00:00
this . world = world ;
2020-08-23 12:52:36 +00:00
}
2022-01-31 15:32:21 +00:00
//New claim
2021-12-03 21:37:35 +00:00
public Claim ( BlockPos pos1 , BlockPos pos2 , ServerPlayer creator ) {
2023-06-11 19:47:12 +00:00
this ( pos1 . getX ( ) , pos2 . getX ( ) , pos1 . getZ ( ) , pos2 . getZ ( ) , Math . min ( pos1 . getY ( ) , pos2 . getY ( ) ) , creator . getUUID ( ) , creator . serverLevel ( ) , PlayerClaimData . get ( creator ) . playerDefaultGroups ( ) . isEmpty ( ) ) ;
2021-05-03 18:03:51 +00:00
PlayerClaimData . get ( creator ) . playerDefaultGroups ( ) . forEach ( ( s , m ) - > m . forEach ( ( perm , bool ) - > this . editPerms ( null , s , perm , bool ? 1 : 0 , true ) ) ) ;
2023-06-11 19:47:12 +00:00
Collection < Claim > all = ClaimStorage . get ( creator . serverLevel ( ) ) . allClaimsFromPlayer ( creator . getUUID ( ) ) ;
2022-01-31 15:32:21 +00:00
String name = String . format ( ConfigHandler . config . defaultClaimName , creator . getName ( ) , all . size ( ) ) ;
if ( ! name . isEmpty ( ) ) {
for ( Claim claim : all ) {
if ( claim . claimName . equals ( name ) ) {
name = name + " # " + all . size ( ) ;
break ;
}
}
}
this . claimName = name ;
if ( ! ConfigHandler . config . defaultEnterMessage . isEmpty ( ) )
2022-07-01 16:26:13 +00:00
this . enterTitle = Component . literal ( String . format ( ConfigHandler . config . defaultEnterMessage , this . claimName ) ) ;
2022-01-31 15:32:21 +00:00
if ( ! ConfigHandler . config . defaultLeaveMessage . isEmpty ( ) )
2022-07-01 16:26:13 +00:00
this . leaveTitle = Component . literal ( String . format ( ConfigHandler . config . defaultLeaveMessage , this . claimName ) ) ;
2021-05-02 19:39:06 +00:00
}
2021-12-03 21:37:35 +00:00
public Claim ( BlockPos pos1 , BlockPos pos2 , UUID creator , ServerLevel world ) {
2020-08-24 19:03:06 +00:00
this ( pos1 . getX ( ) , pos2 . getX ( ) , pos1 . getZ ( ) , pos2 . getZ ( ) , Math . min ( pos1 . getY ( ) , pos2 . getY ( ) ) , creator , world ) ;
2020-08-23 12:52:36 +00:00
}
2022-01-31 15:32:21 +00:00
//Griefprevention parsing
2021-12-03 21:37:35 +00:00
public Claim ( int x1 , int x2 , int z1 , int z2 , int minY , UUID creator , ServerLevel world ) {
2021-05-02 19:39:06 +00:00
this ( x1 , x2 , z1 , z2 , minY , creator , world , true ) ;
}
2021-12-03 21:37:35 +00:00
public Claim ( int x1 , int x2 , int z1 , int z2 , int minY , UUID creator , ServerLevel world , boolean setDefaultGroups ) {
2020-08-23 12:52:36 +00:00
this . minX = Math . min ( x1 , x2 ) ;
this . minZ = Math . min ( z1 , z2 ) ;
this . maxX = Math . max ( x1 , x2 ) ;
this . maxZ = Math . max ( z1 , z2 ) ;
2021-12-19 15:05:45 +00:00
this . minY = Math . max ( world . getMinBuildHeight ( ) , minY ) ;
2020-08-23 12:52:36 +00:00
this . owner = creator ;
2020-08-24 19:03:06 +00:00
this . world = world ;
2021-06-12 21:21:38 +00:00
this . homePos = this . getInitCenterPos ( ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2021-04-11 23:50:31 +00:00
PermissionRegistry . getPerms ( ) . stream ( ) . filter ( perm - > perm . defaultVal ) . forEach ( perm - > this . globalPerm . put ( perm , true ) ) ;
2021-12-27 18:00:07 +00:00
ConfigHandler . config . getGloballyDefinedVals ( world ) . forEach ( e - > this . globalPerm . put ( e . getKey ( ) , e . getValue ( ) . getValue ( ) ) ) ;
2021-05-02 19:39:06 +00:00
if ( setDefaultGroups )
2021-05-03 18:03:51 +00:00
ConfigHandler . config . defaultGroups . forEach ( ( s , m ) - > m . forEach ( ( perm , bool ) - > this . editPerms ( null , s , perm , bool ? 1 : 0 , true ) ) ) ;
2020-08-23 12:52:36 +00:00
}
2021-12-03 21:37:35 +00:00
public static Claim fromJson ( JsonObject obj , UUID owner , ServerLevel world ) {
2020-08-24 19:03:06 +00:00
Claim claim = new Claim ( world ) ;
2020-08-23 12:52:36 +00:00
claim . readJson ( obj , owner ) ;
2021-07-31 15:56:43 +00:00
ClaimUpdater . updateClaim ( claim ) ;
2020-08-23 12:52:36 +00:00
return claim ;
}
2021-06-12 21:21:38 +00:00
private BlockPos getInitCenterPos ( ) {
2023-05-06 13:58:42 +00:00
BlockPos center = BlockPos . containing ( this . minX + ( this . maxX - this . minX ) * 0 . 5 , 0 , this . minZ + ( this . maxZ - this . minZ ) * 0 . 5 ) ;
2023-06-11 19:47:12 +00:00
int y = this . world . getChunk ( center . getX ( ) > > 4 , center . getZ ( ) > > 4 , ChunkStatus . FULL ) . getHeight ( Heightmap . Types . MOTION_BLOCKING , center . getX ( ) & 15 , center . getZ ( ) & 15 ) ;
2021-06-12 21:21:38 +00:00
return new BlockPos ( center . getX ( ) , y + 1 , center . getZ ( ) ) ;
}
2021-06-29 15:28:48 +00:00
private BlockPos getDefaultCenterPos ( ) {
2023-05-06 13:58:42 +00:00
BlockPos center = BlockPos . containing ( this . minX + ( this . maxX - this . minX ) * 0 . 5 , 0 , this . minZ + ( this . maxZ - this . minZ ) * 0 . 5 ) ;
2021-06-29 15:28:48 +00:00
return new BlockPos ( center . getX ( ) , 255 , center . getZ ( ) ) ;
}
2020-08-23 12:52:36 +00:00
public void setClaimID ( UUID uuid ) {
this . claimID = uuid ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-23 12:52:36 +00:00
}
2020-09-02 13:36:58 +00:00
public void extendDownwards ( BlockPos pos ) {
2020-08-26 14:39:44 +00:00
this . minY = pos . getY ( ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-26 14:39:44 +00:00
}
2020-08-23 12:52:36 +00:00
public UUID getClaimID ( ) {
return this . claimID ;
}
2021-06-10 15:29:30 +00:00
public String getClaimName ( ) {
2020-11-28 19:53:29 +00:00
return this . claimName ;
}
2021-06-10 15:29:30 +00:00
public void setClaimName ( String name ) {
2020-11-28 19:53:29 +00:00
this . claimName = name ;
this . setDirty ( true ) ;
2022-11-24 14:26:26 +00:00
WebmapCalls . changeClaimName ( this ) ;
2020-11-28 19:53:29 +00:00
}
2020-08-23 12:52:36 +00:00
public UUID getOwner ( ) {
return this . owner ;
}
2021-12-03 21:37:35 +00:00
public Optional < ServerPlayer > getOwnerPlayer ( ) {
2021-07-02 18:01:27 +00:00
if ( this . getOwner ( ) ! = null )
2021-12-03 21:37:35 +00:00
return Optional . ofNullable ( this . world . getServer ( ) . getPlayerList ( ) . getPlayer ( this . getOwner ( ) ) ) ;
2021-07-02 18:01:27 +00:00
return Optional . empty ( ) ;
}
2021-12-03 21:37:35 +00:00
public ServerLevel getWorld ( ) {
2020-10-30 14:09:57 +00:00
return this . world ;
}
2020-08-25 17:43:52 +00:00
public Claim parentClaim ( ) {
if ( this . parent = = null )
2020-08-24 19:03:06 +00:00
return null ;
2020-08-25 17:43:52 +00:00
if ( this . parentClaim = = null ) {
2020-08-24 19:03:06 +00:00
ClaimStorage storage = ClaimStorage . get ( this . world ) ;
2020-08-25 19:06:36 +00:00
this . parentClaim = storage . getFromUUID ( this . parent ) ;
2020-08-24 19:03:06 +00:00
}
return this . parentClaim ;
}
2020-08-25 17:43:52 +00:00
public void copySizes ( Claim claim ) {
2020-08-24 19:03:06 +00:00
this . minX = claim . minX ;
this . maxX = claim . maxX ;
this . minZ = claim . minZ ;
this . maxZ = claim . maxZ ;
this . minY = claim . minY ;
this . removed = false ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-24 19:03:06 +00:00
}
2021-12-03 21:37:35 +00:00
public void toggleAdminClaim ( ServerPlayer player , boolean flag ) {
2020-09-02 13:36:58 +00:00
if ( ! flag )
2021-12-03 21:37:35 +00:00
this . transferOwner ( player . getUUID ( ) ) ;
2020-09-02 13:19:31 +00:00
else {
this . owner = null ;
this . subClaims . forEach ( claim - > claim . owner = null ) ;
}
this . setDirty ( true ) ;
2020-08-23 21:18:18 +00:00
}
2020-09-02 13:36:58 +00:00
public boolean isAdminClaim ( ) {
return this . owner = = null ;
2020-09-02 13:19:31 +00:00
}
2020-09-03 16:00:37 +00:00
public void transferOwner ( UUID player ) {
this . owner = player ;
this . subClaims . forEach ( claim - > claim . owner = player ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-26 23:17:09 +00:00
}
2020-08-23 12:52:36 +00:00
public int getPlane ( ) {
2020-12-11 13:17:24 +00:00
return ( this . maxX - this . minX + 1 ) * ( this . maxZ - this . minZ + 1 ) ;
2020-08-23 12:52:36 +00:00
}
/ * *
* @return The claims dimension in order : x , X , z , Z , y
* /
public int [ ] getDimensions ( ) {
return new int [ ] { this . minX , this . maxX , this . minZ , this . maxZ , this . minY } ;
}
public boolean insideClaim ( BlockPos pos ) {
return this . minX < = pos . getX ( ) & & this . maxX > = pos . getX ( ) & & this . minZ < = pos . getZ ( ) & & this . maxZ > = pos . getZ ( ) & & this . minY < = pos . getY ( ) ;
}
public boolean intersects ( Claim other ) {
2020-09-15 11:54:00 +00:00
return this . minX < = other . maxX & & this . maxX > = other . minX & & this . minZ < = other . maxZ & & this . maxZ > = other . minZ ;
2020-08-23 12:52:36 +00:00
}
2021-12-03 21:37:35 +00:00
public boolean intersects ( AABB box ) {
2021-06-13 01:56:31 +00:00
return this . minX < box . maxX & & this . maxX + 1 > box . minX & & this . minZ < box . maxZ & & this . maxZ + 1 > box . minZ & & box . maxY > = this . minY ;
2021-06-12 22:21:31 +00:00
}
2020-08-23 12:52:36 +00:00
public boolean isCorner ( BlockPos pos ) {
return ( pos . getX ( ) = = this . minX & & pos . getZ ( ) = = this . minZ ) | | ( pos . getX ( ) = = this . minX & & pos . getZ ( ) = = this . maxZ )
| | ( pos . getX ( ) = = this . maxX & & pos . getZ ( ) = = this . minZ ) | | ( pos . getX ( ) = = this . maxX & & pos . getZ ( ) = = this . maxZ ) ;
}
public void remove ( ) {
this . removed = true ;
}
public boolean isRemoved ( ) {
return this . removed ;
}
2021-06-10 17:09:59 +00:00
@Override
2021-12-03 21:37:35 +00:00
public boolean canInteract ( ServerPlayer player , ClaimPermission perm , BlockPos pos , boolean message ) {
2022-01-30 18:21:43 +00:00
boolean realPlayer = player ! = null & & player . getClass ( ) . equals ( ServerPlayer . class ) ;
message = message & & realPlayer ; //dont send messages to fake players
//Delegate interaction to FAKEPLAYER perm if a fake player
if ( player ! = null & & ! realPlayer ) {
//Some mods use the actual user/placer/owner whatever of the fakeplayer. E.g. ComputerCraft
//For those mods we dont pass them as fake players
2023-03-11 17:17:18 +00:00
if ( this . fakePlayers . contains ( player . getUUID ( ) ) )
return true ;
2022-01-30 18:21:43 +00:00
if ( ! player . getUUID ( ) . equals ( this . owner ) & & ! this . playersGroups . containsKey ( player . getUUID ( ) ) ) {
perm = PermissionRegistry . FAKEPLAYER ;
}
}
2022-02-27 16:25:31 +00:00
InteractionResult res = ClaimPermissionCheck . INSTANCE . check ( player , perm , pos ) ;
2021-12-03 21:37:35 +00:00
if ( res ! = InteractionResult . PASS )
return res ! = InteractionResult . FAIL ;
2020-11-28 19:53:29 +00:00
if ( perm ! = null ) {
ClaimPermission . PermissionFlag flag = perm . test . test ( this , player , pos ) ;
if ( flag ! = ClaimPermission . PermissionFlag . PASS ) {
if ( flag = = ClaimPermission . PermissionFlag . NO ) {
if ( message )
2022-01-30 17:31:45 +00:00
player . displayClientMessage ( PermHelper . simpleColoredText ( ConfigHandler . langManager . get ( " noPermissionSimple " ) , ChatFormatting . DARK_RED ) , true ) ;
2023-03-11 17:17:18 +00:00
if ( perm = = PermissionRegistry . FAKEPLAYER )
this . getOwnerPlayer ( ) . ifPresent ( p - > PlayerClaimData . get ( p ) . notifyFakePlayerInteraction ( player , pos , this ) ) ;
2020-11-28 19:53:29 +00:00
return false ;
}
return true ;
}
}
2021-08-13 09:20:55 +00:00
if ( ! this . isAdminClaim ( ) ) {
Config . GlobalType global = ConfigHandler . config . getGlobal ( this . world , perm ) ;
2021-09-02 12:07:56 +00:00
if ( ! global . canModify ( ) ) {
2021-08-13 09:20:55 +00:00
if ( global . getValue ( ) | | ( player ! = null & & this . isAdminIgnore ( player ) ) )
return true ;
if ( message )
2022-01-30 17:31:45 +00:00
player . displayClientMessage ( PermHelper . simpleColoredText ( ConfigHandler . langManager . get ( " noPermissionSimple " ) , ChatFormatting . DARK_RED ) , true ) ;
2023-03-11 17:17:18 +00:00
if ( perm = = PermissionRegistry . FAKEPLAYER )
this . getOwnerPlayer ( ) . ifPresent ( p - > PlayerClaimData . get ( p ) . notifyFakePlayerInteraction ( player , pos , this ) ) ;
2021-08-13 09:20:55 +00:00
return false ;
}
2021-09-02 12:07:56 +00:00
if ( ConfigHandler . config . offlineProtectActivation ! = - 1 & & ( LogoutTracker . getInstance ( this . world . getServer ( ) ) . justLoggedOut ( this . getOwner ( ) ) | | this . getOwnerPlayer ( ) . isPresent ( ) ) ) {
2021-08-13 09:20:55 +00:00
return global = = Config . GlobalType . NONE | | global . getValue ( ) ;
}
2020-10-30 14:09:57 +00:00
}
2020-11-28 19:53:29 +00:00
if ( PermissionRegistry . globalPerms ( ) . contains ( perm ) ) {
2020-08-23 12:52:36 +00:00
for ( Claim claim : this . subClaims ) {
if ( claim . insideClaim ( pos ) ) {
2020-08-24 19:03:06 +00:00
return claim . canInteract ( player , perm , pos , message ) ;
2020-08-23 12:52:36 +00:00
}
}
2020-08-25 17:43:52 +00:00
if ( this . hasPerm ( perm ) )
2020-08-24 19:03:06 +00:00
return true ;
2020-08-25 17:43:52 +00:00
if ( message )
2022-01-30 17:31:45 +00:00
player . displayClientMessage ( PermHelper . simpleColoredText ( ConfigHandler . langManager . get ( " noPermissionSimple " ) , ChatFormatting . DARK_RED ) , true ) ;
2023-03-11 17:17:18 +00:00
if ( perm = = PermissionRegistry . FAKEPLAYER )
this . getOwnerPlayer ( ) . ifPresent ( p - > PlayerClaimData . get ( p ) . notifyFakePlayerInteraction ( player , pos , this ) ) ;
2020-08-24 19:03:06 +00:00
return false ;
2020-08-23 12:52:36 +00:00
}
2021-12-03 21:37:35 +00:00
if ( this . isAdminIgnore ( player ) | | player . getUUID ( ) . equals ( this . owner ) )
2020-08-23 12:52:36 +00:00
return true ;
2020-11-28 19:53:29 +00:00
if ( perm ! = PermissionRegistry . EDITCLAIM & & perm ! = PermissionRegistry . EDITPERMS )
2020-09-03 16:00:37 +00:00
for ( Claim claim : this . subClaims ) {
if ( claim . insideClaim ( pos ) ) {
2020-08-24 19:03:06 +00:00
return claim . canInteract ( player , perm , pos , message ) ;
2020-09-03 16:00:37 +00:00
}
2020-08-23 12:52:36 +00:00
}
2021-12-03 21:37:35 +00:00
if ( this . playersGroups . containsKey ( player . getUUID ( ) ) ) {
Map < ClaimPermission , Boolean > map = this . permissions . get ( this . playersGroups . get ( player . getUUID ( ) ) ) ;
2020-08-24 19:03:06 +00:00
if ( map ! = null & & map . containsKey ( perm ) ) {
2020-08-25 17:43:52 +00:00
if ( map . get ( perm ) )
2020-08-24 19:03:06 +00:00
return true ;
2020-08-25 17:43:52 +00:00
if ( message )
2022-01-30 17:31:45 +00:00
player . displayClientMessage ( PermHelper . simpleColoredText ( ConfigHandler . langManager . get ( " noPermissionSimple " ) , ChatFormatting . DARK_RED ) , true ) ;
2023-03-11 17:17:18 +00:00
if ( perm = = PermissionRegistry . FAKEPLAYER )
this . getOwnerPlayer ( ) . ifPresent ( p - > PlayerClaimData . get ( p ) . notifyFakePlayerInteraction ( player , pos , this ) ) ;
2020-08-24 19:03:06 +00:00
return false ;
}
2020-08-23 12:52:36 +00:00
}
2020-08-25 17:43:52 +00:00
if ( this . hasPerm ( perm ) )
2020-08-24 19:03:06 +00:00
return true ;
2020-08-25 17:43:52 +00:00
if ( message )
2022-01-30 17:31:45 +00:00
player . displayClientMessage ( PermHelper . simpleColoredText ( ConfigHandler . langManager . get ( " noPermissionSimple " ) , ChatFormatting . DARK_RED ) , true ) ;
2023-03-11 17:17:18 +00:00
if ( perm = = PermissionRegistry . FAKEPLAYER )
this . getOwnerPlayer ( ) . ifPresent ( p - > PlayerClaimData . get ( p ) . notifyFakePlayerInteraction ( player , pos , this ) ) ;
2020-08-24 19:03:06 +00:00
return false ;
}
2021-12-03 21:37:35 +00:00
private boolean isAdminIgnore ( ServerPlayer player ) {
return player = = null | | ( ( this . isAdminClaim ( ) & & player . hasPermissions ( 2 ) ) | | PlayerClaimData . get ( player ) . isAdminIgnoreClaim ( ) ) ;
2020-10-30 14:09:57 +00:00
}
2020-08-24 19:03:06 +00:00
/ * *
* @return - 1 for default , 0 for false , 1 for true
* /
2020-11-28 19:53:29 +00:00
public int permEnabled ( ClaimPermission perm ) {
2020-08-25 17:43:52 +00:00
return ! this . globalPerm . containsKey ( perm ) ? - 1 : this . globalPerm . get ( perm ) ? 1 : 0 ;
2020-08-23 12:52:36 +00:00
}
2020-11-28 19:53:29 +00:00
private boolean hasPerm ( ClaimPermission perm ) {
2020-08-25 17:43:52 +00:00
if ( this . parentClaim ( ) = = null )
2020-08-24 19:03:06 +00:00
return this . permEnabled ( perm ) = = 1 ;
2020-08-25 17:43:52 +00:00
if ( this . permEnabled ( perm ) = = - 1 )
return this . parentClaim ( ) . permEnabled ( perm ) = = 1 ;
2020-08-24 19:03:06 +00:00
return this . permEnabled ( perm ) = = 1 ;
}
private UUID generateUUID ( ) {
UUID uuid = UUID . randomUUID ( ) ;
for ( Claim claim : this . subClaims )
2020-08-25 17:43:52 +00:00
if ( claim . claimID . equals ( uuid ) ) {
2020-10-30 14:38:24 +00:00
return this . generateUUID ( ) ;
2020-08-24 19:03:06 +00:00
}
return uuid ;
2020-08-23 12:52:36 +00:00
}
2020-08-24 20:32:29 +00:00
public Set < Claim > tryCreateSubClaim ( BlockPos pos1 , BlockPos pos2 ) {
2022-05-12 20:05:58 +00:00
//No sub sub claims
if ( this . parentClaim ( ) ! = null )
return Set . of ( this . parentClaim ( ) ) ;
2020-08-24 19:03:06 +00:00
Claim sub = new Claim ( pos1 , new BlockPos ( pos2 . getX ( ) , 0 , pos2 . getZ ( ) ) , this . owner , this . world ) ;
sub . setClaimID ( this . generateUUID ( ) ) ;
2021-05-02 16:06:56 +00:00
Set < Claim > conflicts = new HashSet < > ( ) ;
2020-08-25 17:43:52 +00:00
for ( Claim other : this . subClaims )
2020-08-23 21:18:18 +00:00
if ( sub . intersects ( other ) ) {
2020-09-15 11:54:00 +00:00
conflicts . add ( other ) ;
2020-08-23 21:18:18 +00:00
}
2020-08-25 17:43:52 +00:00
if ( conflicts . isEmpty ( ) ) {
2020-08-24 20:32:29 +00:00
sub . parent = this . claimID ;
sub . parentClaim = this ;
this . subClaims . add ( sub ) ;
2022-05-12 20:05:58 +00:00
//Copy parent claims perms
sub . permissions . clear ( ) ;
sub . permissions . putAll ( this . permissions ) ;
sub . playersGroups . clear ( ) ;
sub . playersGroups . putAll ( this . playersGroups ) ;
sub . potions . clear ( ) ;
sub . potions . putAll ( this . potions ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-24 20:32:29 +00:00
}
return conflicts ;
2020-08-23 12:52:36 +00:00
}
2020-08-25 17:43:52 +00:00
public void addSubClaimGriefprevention ( Claim claim ) {
2020-08-24 19:03:06 +00:00
claim . setClaimID ( this . generateUUID ( ) ) ;
claim . parent = this . claimID ;
claim . parentClaim = this ;
this . subClaims . add ( claim ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
public Claim getSubClaim ( BlockPos pos ) {
for ( Claim claim : this . subClaims )
if ( claim . insideClaim ( pos ) )
return claim ;
return null ;
}
2020-08-25 17:43:52 +00:00
public boolean deleteSubClaim ( Claim claim ) {
2020-08-25 19:50:41 +00:00
claim . remove ( ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-24 19:03:06 +00:00
return this . subClaims . remove ( claim ) ;
}
2020-08-25 17:43:52 +00:00
public List < Claim > getAllSubclaims ( ) {
2020-08-23 21:18:18 +00:00
return ImmutableList . copyOf ( this . subClaims ) ;
}
2020-08-25 17:43:52 +00:00
public Set < Claim > resizeSubclaim ( Claim claim , BlockPos from , BlockPos to ) {
2020-08-24 19:03:06 +00:00
int [ ] dims = claim . getDimensions ( ) ;
2020-08-25 17:43:52 +00:00
BlockPos opposite = new BlockPos ( dims [ 0 ] = = from . getX ( ) ? dims [ 1 ] : dims [ 0 ] , dims [ 4 ] , dims [ 2 ] = = from . getZ ( ) ? dims [ 3 ] : dims [ 2 ] ) ;
2020-08-24 19:03:06 +00:00
Claim newClaim = new Claim ( opposite , to , claim . claimID , this . world ) ;
2021-05-02 16:06:56 +00:00
Set < Claim > conflicts = new HashSet < > ( ) ;
2020-08-25 17:43:52 +00:00
for ( Claim other : this . subClaims )
2020-08-24 19:03:06 +00:00
if ( ! claim . equals ( other ) & & newClaim . intersects ( other ) )
2020-08-24 20:32:29 +00:00
conflicts . add ( other ) ;
2020-08-25 19:50:41 +00:00
if ( conflicts . isEmpty ( ) ) {
2020-08-24 20:32:29 +00:00
claim . copySizes ( newClaim ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-25 19:50:41 +00:00
}
2020-08-24 20:32:29 +00:00
return conflicts ;
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
public boolean setPlayerGroup ( UUID player , String group , boolean force ) {
2020-09-02 13:19:31 +00:00
if ( player . equals ( this . owner ) )
2020-08-23 21:18:18 +00:00
return false ;
2020-08-23 12:52:36 +00:00
if ( group = = null ) {
this . playersGroups . remove ( player ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-23 12:52:36 +00:00
return true ;
}
if ( ! this . playersGroups . containsKey ( player ) | | force ) {
this . playersGroups . put ( player , group ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-23 12:52:36 +00:00
return true ;
}
return false ;
}
2023-03-11 17:17:18 +00:00
public boolean modifyFakePlayerUUID ( UUID uuid , boolean remove ) {
if ( remove )
return this . fakePlayers . remove ( uuid ) ;
return this . fakePlayers . add ( uuid ) ;
}
2020-08-23 12:52:36 +00:00
public List < String > playersFromGroup ( MinecraftServer server , String group ) {
2021-05-02 16:06:56 +00:00
List < UUID > l = new ArrayList < > ( ) ;
2020-08-23 12:52:36 +00:00
this . playersGroups . forEach ( ( uuid , g ) - > {
if ( g . equals ( group ) )
l . add ( uuid ) ;
} ) ;
2021-05-02 16:06:56 +00:00
List < String > names = new ArrayList < > ( ) ;
2021-12-03 21:37:35 +00:00
l . forEach ( uuid - > server . getProfileCache ( ) . get ( uuid ) . ifPresent ( prof - > names . add ( prof . getName ( ) ) ) ) ;
2020-08-23 12:52:36 +00:00
names . sort ( null ) ;
return names ;
}
2023-03-11 17:17:18 +00:00
public List < String > getAllowedFakePlayerUUID ( ) {
return this . fakePlayers . stream ( ) . map ( UUID : : toString ) . toList ( ) ;
}
2021-12-03 21:37:35 +00:00
public boolean editGlobalPerms ( ServerPlayer player , ClaimPermission toggle , int mode ) {
if ( ( player ! = null & & ! this . canInteract ( player , PermissionRegistry . EDITPERMS , player . blockPosition ( ) ) ) | | ( ! this . isAdminClaim ( ) & & ConfigHandler . config . globallyDefined ( this . world , toggle ) ) )
2020-10-30 14:09:57 +00:00
return false ;
2020-08-24 19:03:06 +00:00
if ( mode > 1 )
mode = - 1 ;
if ( mode = = - 1 )
2020-08-23 12:52:36 +00:00
this . globalPerm . remove ( toggle ) ;
else
2020-08-24 19:03:06 +00:00
this . globalPerm . put ( toggle , mode = = 1 ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-10-30 14:09:57 +00:00
return true ;
2020-08-23 12:52:36 +00:00
}
2021-12-03 21:37:35 +00:00
public boolean editPerms ( ServerPlayer player , String group , ClaimPermission perm , int mode ) {
2020-09-02 13:19:31 +00:00
return this . editPerms ( player , group , perm , mode , false ) ;
}
2020-09-02 13:36:58 +00:00
2020-08-23 12:52:36 +00:00
/ * *
* Edit the permissions for a group . If not defined for the group creates a new default permission map for that group
*
* @param mode - 1 = makes it resort to the global perm , 0 = deny perm , 1 = allow perm
* @return If editing was successful or not
* /
2021-12-03 21:37:35 +00:00
public boolean editPerms ( ServerPlayer player , String group , ClaimPermission perm , int mode , boolean alwaysCan ) {
2020-11-28 19:53:29 +00:00
if ( PermissionRegistry . globalPerms ( ) . contains ( perm ) | | ( ! this . isAdminClaim ( ) & & ConfigHandler . config . globallyDefined ( this . world , perm ) ) )
2020-09-02 13:19:31 +00:00
return false ;
2021-12-03 21:37:35 +00:00
if ( alwaysCan | | this . canInteract ( player , PermissionRegistry . EDITPERMS , player . blockPosition ( ) ) ) {
2020-08-23 12:52:36 +00:00
if ( mode > 1 )
mode = - 1 ;
2020-08-23 13:16:26 +00:00
boolean has = this . permissions . containsKey ( group ) ;
2021-05-02 16:06:56 +00:00
Map < ClaimPermission , Boolean > perms = has ? this . permissions . get ( group ) : new HashMap < > ( ) ;
2020-08-23 12:52:36 +00:00
if ( mode = = - 1 )
perms . remove ( perm ) ;
else
perms . put ( perm , mode = = 1 ) ;
if ( ! has )
this . permissions . put ( group , perms ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-23 12:52:36 +00:00
return true ;
}
return false ;
}
2021-12-03 21:37:35 +00:00
public boolean removePermGroup ( ServerPlayer player , String group ) {
if ( this . canInteract ( player , PermissionRegistry . EDITPERMS , player . blockPosition ( ) ) ) {
2020-08-23 12:52:36 +00:00
this . permissions . remove ( group ) ;
2021-05-02 16:06:56 +00:00
List < UUID > toRemove = new ArrayList < > ( ) ;
2020-08-23 12:52:36 +00:00
this . playersGroups . forEach ( ( uuid , g ) - > {
if ( g . equals ( group ) )
toRemove . add ( uuid ) ;
} ) ;
2020-08-23 13:16:26 +00:00
toRemove . forEach ( this . playersGroups : : remove ) ;
2020-09-02 13:19:31 +00:00
this . setDirty ( true ) ;
2020-08-23 12:52:36 +00:00
return true ;
}
return false ;
}
2020-11-28 19:53:29 +00:00
public int groupHasPerm ( String rank , ClaimPermission perm ) {
2020-08-23 12:52:36 +00:00
if ( ! this . permissions . containsKey ( rank ) | | ! this . permissions . get ( rank ) . containsKey ( perm ) )
return - 1 ;
return this . permissions . get ( rank ) . get ( perm ) ? 1 : 0 ;
}
public List < String > groups ( ) {
2021-05-02 16:18:16 +00:00
List < String > l = new ArrayList < > ( this . permissions . keySet ( ) ) ;
2020-08-23 12:52:36 +00:00
l . sort ( null ) ;
return l ;
}
2021-06-12 21:21:38 +00:00
public boolean setHomePos ( BlockPos homePos ) {
if ( this . insideClaim ( homePos ) ) {
this . homePos = homePos ;
2021-06-12 22:21:31 +00:00
this . setDirty ( true ) ;
2021-06-12 21:21:38 +00:00
return true ;
}
return false ;
}
2021-12-03 21:37:35 +00:00
public void addPotion ( MobEffect effect , int amplifier ) {
2021-06-12 23:49:10 +00:00
this . potions . put ( effect , amplifier ) ;
this . setDirty ( true ) ;
}
2021-12-03 21:37:35 +00:00
public void removePotion ( MobEffect effect ) {
2021-06-12 23:49:10 +00:00
this . potions . remove ( effect ) ;
this . setDirty ( true ) ;
}
2021-12-03 21:37:35 +00:00
public Map < MobEffect , Integer > getPotions ( ) {
2021-06-12 23:49:10 +00:00
return this . potions ;
}
2021-12-03 21:37:35 +00:00
public void applyEffects ( ServerPlayer player ) {
2023-06-11 19:47:12 +00:00
if ( player . level ( ) . getGameTime ( ) % 80 = = 0 )
2022-07-27 16:14:39 +00:00
this . potions . forEach ( ( effect , amp ) - > player . forceAddEffect ( new MobEffectInstance ( effect , effect = = MobEffects . NIGHT_VISION ? 400 : 200 , amp - 1 , true , false ) , null ) ) ;
2021-06-12 23:49:10 +00:00
}
2021-06-12 21:21:38 +00:00
public BlockPos getHomePos ( ) {
return this . homePos ;
}
2021-12-03 21:37:35 +00:00
public void setEnterTitle ( Component title , Component sub ) {
2022-07-27 16:14:39 +00:00
if ( title ! = null & & title . getString ( ) . equals ( " $empty " ) )
2022-01-30 18:47:08 +00:00
title = null ;
2022-07-27 16:14:39 +00:00
if ( sub ! = null & & sub . getString ( ) . equals ( " $empty " ) )
2022-02-02 15:29:29 +00:00
sub = null ;
2021-09-14 15:26:41 +00:00
this . enterTitle = title ;
this . enterSubtitle = sub ;
this . setDirty ( true ) ;
}
2021-12-03 21:37:35 +00:00
public void setLeaveTitle ( Component title , Component sub ) {
2022-07-27 16:14:39 +00:00
if ( title ! = null & & title . getString ( ) . equals ( " $empty " ) )
2022-01-30 18:47:08 +00:00
title = null ;
2022-07-27 16:14:39 +00:00
if ( sub ! = null & & sub . getString ( ) . equals ( " $empty " ) )
2022-02-02 15:29:29 +00:00
sub = null ;
2021-09-14 15:26:41 +00:00
this . leaveTitle = title ;
this . leaveSubtitle = sub ;
this . setDirty ( true ) ;
}
2022-11-24 21:41:39 +00:00
private void displayTitleMessage ( ServerPlayer player , @Nullable Component title , @Nullable Component subtitle ) {
if ( title = = null ) return ;
2022-11-24 16:20:37 +00:00
if ( ConfigHandler . config . claimDisplayActionBar ) {
if ( subtitle ! = null ) {
2022-11-24 21:41:39 +00:00
MutableComponent message = title . copy ( ) . append ( Component . literal ( " | " ) . setStyle ( Style . EMPTY . withColor ( ChatFormatting . WHITE ) ) ) . append ( subtitle ) ;
2022-11-26 19:47:39 +00:00
player . displayClientMessage ( message , true ) ;
2022-11-24 16:20:37 +00:00
return ;
}
2022-11-26 19:47:39 +00:00
player . displayClientMessage ( title , true ) ;
2022-11-24 16:20:37 +00:00
return ;
}
2022-11-24 21:41:39 +00:00
player . connection . send ( new ClientboundSetTitleTextPacket ( title ) ) ;
2022-11-24 16:20:37 +00:00
if ( subtitle ! = null ) {
2022-11-24 21:41:39 +00:00
player . connection . send ( new ClientboundSetSubtitleTextPacket ( subtitle ) ) ;
2021-09-14 15:26:41 +00:00
}
}
2022-11-24 16:20:37 +00:00
public void displayEnterTitle ( ServerPlayer player ) {
2023-03-11 17:17:18 +00:00
this . displayTitleMessage ( player , this . enterTitle , this . enterSubtitle ) ;
2022-11-24 16:20:37 +00:00
}
2021-12-03 21:37:35 +00:00
public void displayLeaveTitle ( ServerPlayer player ) {
2023-03-11 17:17:18 +00:00
this . displayTitleMessage ( player , this . leaveTitle , this . leaveSubtitle ) ;
2021-09-14 15:26:41 +00:00
}
2020-08-25 19:50:41 +00:00
/ * *
* Only marks non sub claims
* /
2020-09-02 13:19:31 +00:00
public void setDirty ( boolean flag ) {
2020-09-02 13:36:58 +00:00
if ( this . parentClaim ( ) ! = null )
2020-09-02 13:19:31 +00:00
this . parentClaim ( ) . setDirty ( flag ) ;
2020-08-25 19:50:41 +00:00
else
2020-09-02 13:19:31 +00:00
this . dirty = flag ;
2020-08-23 12:52:36 +00:00
}
public boolean isDirty ( ) {
return this . dirty ;
}
public void readJson ( JsonObject obj , UUID uuid ) {
2021-06-10 15:39:39 +00:00
try {
this . claimID = UUID . fromString ( obj . get ( " ID " ) . getAsString ( ) ) ;
this . claimName = ConfigHandler . fromJson ( obj , " Name " , " " ) ;
JsonArray pos = obj . getAsJsonArray ( " PosxXzZY " ) ;
this . minX = pos . get ( 0 ) . getAsInt ( ) ;
this . maxX = pos . get ( 1 ) . getAsInt ( ) ;
this . minZ = pos . get ( 2 ) . getAsInt ( ) ;
this . maxZ = pos . get ( 3 ) . getAsInt ( ) ;
this . minY = pos . get ( 4 ) . getAsInt ( ) ;
2021-06-12 21:21:38 +00:00
JsonArray home = ConfigHandler . arryFromJson ( obj , " Home " ) ;
if ( home . size ( ) ! = 3 )
2021-06-29 15:28:48 +00:00
this . homePos = this . getDefaultCenterPos ( ) ;
2021-06-12 21:21:38 +00:00
else {
this . homePos = new BlockPos ( home . get ( 0 ) . getAsInt ( ) , home . get ( 1 ) . getAsInt ( ) , home . get ( 2 ) . getAsInt ( ) ) ;
}
2021-09-15 12:32:24 +00:00
String message = ConfigHandler . fromJson ( obj , " EnterTitle " , " " ) ;
2021-09-21 09:56:57 +00:00
if ( ! message . isEmpty ( ) )
2021-12-03 21:37:35 +00:00
this . enterTitle = Component . Serializer . fromJson ( message ) ;
2022-01-31 15:32:21 +00:00
else
this . enterTitle = null ;
2021-09-15 12:32:24 +00:00
message = ConfigHandler . fromJson ( obj , " EnterSubtitle " , " " ) ;
2021-09-21 09:56:57 +00:00
if ( ! message . isEmpty ( ) )
2021-12-03 21:37:35 +00:00
this . enterSubtitle = Component . Serializer . fromJson ( message ) ;
2022-01-31 15:32:21 +00:00
else
this . enterSubtitle = null ;
2021-09-15 12:32:24 +00:00
message = ConfigHandler . fromJson ( obj , " LeaveTitle " , " " ) ;
2021-09-21 09:56:57 +00:00
if ( ! message . isEmpty ( ) )
2021-12-03 21:37:35 +00:00
this . leaveTitle = Component . Serializer . fromJson ( message ) ;
2022-01-31 15:32:21 +00:00
else
this . leaveTitle = null ;
2021-09-15 12:32:24 +00:00
message = ConfigHandler . fromJson ( obj , " LeaveSubtitle " , " " ) ;
2021-09-21 09:56:57 +00:00
if ( ! message . isEmpty ( ) )
2021-12-03 21:37:35 +00:00
this . leaveSubtitle = Component . Serializer . fromJson ( message ) ;
2022-01-31 15:32:21 +00:00
else
this . leaveSubtitle = null ;
2021-06-12 23:49:10 +00:00
JsonObject potion = ConfigHandler . fromJson ( obj , " Potions " ) ;
2022-02-27 16:25:31 +00:00
potion . entrySet ( ) . forEach ( e - > this . potions . put ( CrossPlatformStuff . INSTANCE . registryStatusEffects ( ) . getFromId ( new ResourceLocation ( e . getKey ( ) ) ) , e . getValue ( ) . getAsInt ( ) ) ) ;
2021-06-10 15:39:39 +00:00
if ( ConfigHandler . fromJson ( obj , " AdminClaim " , false ) )
this . owner = null ;
else
this . owner = uuid ;
this . globalPerm . clear ( ) ;
this . permissions . clear ( ) ;
this . subClaims . clear ( ) ;
if ( obj . has ( " Parent " ) )
this . parent = UUID . fromString ( obj . get ( " Parent " ) . getAsString ( ) ) ;
if ( obj . has ( " GlobalPerms " ) ) {
if ( this . parent = = null ) {
obj . getAsJsonArray ( " GlobalPerms " ) . forEach ( perm - > {
try {
this . globalPerm . put ( PermissionRegistry . get ( perm . getAsString ( ) ) , true ) ;
} catch ( NullPointerException e ) {
Flan . logger . error ( " Error reading permission {} from json for claim {} belonging to {}. No such permission exist " , perm . getAsString ( ) , this . claimID , this . owner ) ;
}
} ) ;
} else {
obj . getAsJsonObject ( " GlobalPerms " ) . entrySet ( ) . forEach ( entry - > {
try {
this . globalPerm . put ( PermissionRegistry . get ( entry . getKey ( ) ) , entry . getValue ( ) . getAsBoolean ( ) ) ;
} catch ( NullPointerException e ) {
Flan . logger . error ( " Error reading permission {} from json for claim {} belonging to {}. No such permission exist " , entry . getKey ( ) , this . claimID , this . owner ) ;
}
} ) ;
}
2020-08-24 19:03:06 +00:00
}
2021-06-10 15:39:39 +00:00
ConfigHandler . fromJson ( obj , " PermGroup " ) . entrySet ( ) . forEach ( key - > {
2021-05-02 16:06:56 +00:00
Map < ClaimPermission , Boolean > map = new HashMap < > ( ) ;
2020-08-23 12:52:36 +00:00
JsonObject group = key . getValue ( ) . getAsJsonObject ( ) ;
2021-05-12 18:06:26 +00:00
group . entrySet ( ) . forEach ( gkey - > {
try {
map . put ( PermissionRegistry . get ( gkey . getKey ( ) ) , gkey . getValue ( ) . getAsBoolean ( ) ) ;
} catch ( NullPointerException e ) {
Flan . logger . error ( " Error reading permission {} from json for claim {} belonging to {}. No such permission exist " , gkey . getKey ( ) , this . claimID , this . owner ) ;
}
} ) ;
2020-08-23 12:52:36 +00:00
this . permissions . put ( key . getKey ( ) , map ) ;
} ) ;
2021-06-10 15:39:39 +00:00
ConfigHandler . fromJson ( obj , " PlayerPerms " ) . entrySet ( )
. forEach ( key - > this . playersGroups . put ( UUID . fromString ( key . getKey ( ) ) , key . getValue ( ) . getAsString ( ) ) ) ;
ConfigHandler . arryFromJson ( obj , " SubClaims " )
. forEach ( sub - > this . subClaims . add ( Claim . fromJson ( sub . getAsJsonObject ( ) , this . owner , this . world ) ) ) ;
2023-03-11 17:17:18 +00:00
ConfigHandler . arryFromJson ( obj , " FakePlayers " )
. forEach ( e - > {
try {
this . fakePlayers . add ( UUID . fromString ( e . getAsString ( ) ) ) ;
} catch ( IllegalArgumentException ignored ) {
}
} ) ;
2021-06-10 15:39:39 +00:00
} catch ( Exception e ) {
throw new IllegalStateException ( " Error reading claim data for claim " + uuid ) ;
2020-08-23 12:52:36 +00:00
}
}
public JsonObject toJson ( JsonObject obj ) {
2020-09-02 13:19:31 +00:00
obj . addProperty ( " ID " , this . claimID . toString ( ) ) ;
2021-06-10 15:29:30 +00:00
obj . addProperty ( " Name " , this . claimName ) ;
2020-08-23 12:52:36 +00:00
JsonArray pos = new JsonArray ( ) ;
pos . add ( this . minX ) ;
pos . add ( this . maxX ) ;
pos . add ( this . minZ ) ;
pos . add ( this . maxZ ) ;
pos . add ( this . minY ) ;
obj . add ( " PosxXzZY " , pos ) ;
2021-06-12 21:21:38 +00:00
JsonArray home = new JsonArray ( ) ;
home . add ( this . homePos . getX ( ) ) ;
home . add ( this . homePos . getY ( ) ) ;
home . add ( this . homePos . getZ ( ) ) ;
obj . add ( " Home " , home ) ;
2021-12-03 21:37:35 +00:00
obj . addProperty ( " EnterTitle " , this . enterTitle = = null ? " " : Component . Serializer . toJson ( this . enterTitle ) ) ;
obj . addProperty ( " EnterSubtitle " , this . enterSubtitle = = null ? " " : Component . Serializer . toJson ( this . enterSubtitle ) ) ;
obj . addProperty ( " LeaveTitle " , this . leaveTitle = = null ? " " : Component . Serializer . toJson ( this . leaveTitle ) ) ;
obj . addProperty ( " LeaveSubtitle " , this . leaveSubtitle = = null ? " " : Component . Serializer . toJson ( this . leaveSubtitle ) ) ;
2021-06-12 23:49:10 +00:00
JsonObject potions = new JsonObject ( ) ;
2022-02-27 16:25:31 +00:00
this . potions . forEach ( ( effect , amp ) - > potions . addProperty ( CrossPlatformStuff . INSTANCE . registryStatusEffects ( ) . getIDFrom ( effect ) . toString ( ) , amp ) ) ;
2021-06-12 23:49:10 +00:00
obj . add ( " Potions " , potions ) ;
2020-08-25 17:43:52 +00:00
if ( this . parent ! = null )
2020-08-24 19:03:06 +00:00
obj . addProperty ( " Parent " , this . parent . toString ( ) ) ;
2020-08-23 12:52:36 +00:00
if ( ! this . globalPerm . isEmpty ( ) ) {
2020-08-24 19:03:06 +00:00
JsonElement gPerm ;
2020-08-25 17:43:52 +00:00
if ( this . parent = = null ) {
2020-08-24 19:03:06 +00:00
gPerm = new JsonArray ( ) ;
this . globalPerm . forEach ( ( perm , bool ) - > {
if ( bool )
2020-11-28 19:53:29 +00:00
( ( JsonArray ) gPerm ) . add ( perm . id ) ;
2020-08-24 19:03:06 +00:00
} ) ;
2020-08-25 17:43:52 +00:00
} else {
2020-08-24 19:03:06 +00:00
gPerm = new JsonObject ( ) ;
2020-11-28 19:53:29 +00:00
this . globalPerm . forEach ( ( perm , bool ) - > ( ( JsonObject ) gPerm ) . addProperty ( perm . id , bool ) ) ;
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
obj . add ( " GlobalPerms " , gPerm ) ;
}
if ( ! this . permissions . isEmpty ( ) ) {
JsonObject perms = new JsonObject ( ) ;
this . permissions . forEach ( ( s , pmap ) - > {
JsonObject group = new JsonObject ( ) ;
2020-11-28 19:53:29 +00:00
pmap . forEach ( ( perm , bool ) - > group . addProperty ( perm . id , bool ) ) ;
2020-08-23 12:52:36 +00:00
perms . add ( s , group ) ;
} ) ;
obj . add ( " PermGroup " , perms ) ;
}
if ( ! this . playersGroups . isEmpty ( ) ) {
JsonObject pl = new JsonObject ( ) ;
this . playersGroups . forEach ( ( uuid , s ) - > pl . addProperty ( uuid . toString ( ) , s ) ) ;
obj . add ( " PlayerPerms " , pl ) ;
}
if ( ! this . subClaims . isEmpty ( ) ) {
JsonArray list = new JsonArray ( ) ;
this . subClaims . forEach ( p - > list . add ( p . toJson ( new JsonObject ( ) ) ) ) ;
obj . add ( " SubClaims " , list ) ;
}
2023-03-11 17:17:18 +00:00
if ( ! this . fakePlayers . isEmpty ( ) ) {
JsonArray list = new JsonArray ( ) ;
this . fakePlayers . forEach ( uuid - > list . add ( uuid . toString ( ) ) ) ;
obj . add ( " FakePlayers " , list ) ;
}
2020-08-23 12:52:36 +00:00
return obj ;
}
@Override
public int hashCode ( ) {
2020-08-25 17:43:52 +00:00
return this . claimID = = null ? Arrays . hashCode ( this . getDimensions ( ) ) : this . claimID . hashCode ( ) ;
2020-08-23 12:52:36 +00:00
}
@Override
public boolean equals ( Object obj ) {
if ( this = = obj )
return true ;
2022-01-08 13:53:13 +00:00
if ( obj instanceof Claim other ) {
2020-08-25 17:43:52 +00:00
if ( this . claimID = = null & & other . claimID = = null )
2020-08-23 21:18:18 +00:00
return Arrays . equals ( this . getDimensions ( ) , ( ( Claim ) obj ) . getDimensions ( ) ) ;
2020-08-25 17:43:52 +00:00
if ( this . claimID ! = null )
2020-08-23 21:18:18 +00:00
return this . claimID . equals ( ( ( Claim ) obj ) . claimID ) ;
}
2020-08-23 12:52:36 +00:00
return false ;
}
@Override
public String toString ( ) {
2020-10-23 22:12:07 +00:00
return String . format ( " Claim:[ID=%s, Owner=%s, from: [x=%d,z=%d], to: [x=%d,z=%d] " , this . claimID ! = null ? this . claimID . toString ( ) : " null " , this . owner ! = null ? this . owner . toString ( ) : " Admin " , this . minX , this . minZ , this . maxX , this . maxZ ) ;
2020-08-23 12:52:36 +00:00
}
2023-03-11 17:17:18 +00:00
public String nameAndPosition ( ) {
if ( this . claimName . isEmpty ( ) )
return String . format ( " [x=%d,z=%d]-[x=%d,z=%d] " , this . minX , this . minZ , this . maxX , this . maxZ ) ;
return String . format ( " %s:[x=%d,z=%d]-[x=%d,z=%d] " , this . claimName , this . minX , this . minZ , this . maxX , this . maxZ ) ;
}
2020-08-23 12:52:36 +00:00
public String formattedClaim ( ) {
2021-06-10 15:29:30 +00:00
if ( this . claimName . isEmpty ( ) )
2022-07-23 22:36:38 +00:00
return String . format ( " [x=%d,z=%d] - [x=%d,z=%d] = %d blocks " , this . minX , this . minZ , this . maxX , this . maxZ , this . getPlane ( ) ) ;
return String . format ( " %s:[x=%d,z=%d] - [x=%d,z=%d] = %d blocks " , this . claimName , this . minX , this . minZ , this . maxX , this . maxZ , this . getPlane ( ) ) ;
2020-08-23 21:18:18 +00:00
}
2021-12-03 21:37:35 +00:00
public List < Component > infoString ( ServerPlayer player , InfoType infoType ) {
boolean perms = this . canInteract ( player , PermissionRegistry . EDITPERMS , player . blockPosition ( ) ) ;
List < Component > l = new ArrayList < > ( ) ;
l . add ( PermHelper . simpleColoredText ( " ============================================= " , ChatFormatting . GREEN ) ) ;
2022-01-08 13:53:13 +00:00
String ownerName = this . isAdminClaim ( ) ? " Admin " : player . getServer ( ) . getProfileCache ( ) . get ( this . owner ) . map ( GameProfile : : getName ) . orElse ( " <UNKNOWN> " ) ;
2021-06-10 17:25:05 +00:00
if ( this . parent = = null ) {
if ( this . claimName . isEmpty ( ) )
2022-01-30 17:31:45 +00:00
l . add ( PermHelper . simpleColoredText ( String . format ( ConfigHandler . langManager . get ( " claimBasicInfo " ) , ownerName , this . minX , this . minZ , this . maxX , this . maxZ , this . subClaims . size ( ) ) , ChatFormatting . GOLD ) ) ;
2021-06-10 17:25:05 +00:00
else
2022-01-30 17:31:45 +00:00
l . add ( PermHelper . simpleColoredText ( String . format ( ConfigHandler . langManager . get ( " claimBasicInfoNamed " ) , ownerName , this . minX , this . minZ , this . maxX , this . maxZ , this . subClaims . size ( ) , this . claimName ) , ChatFormatting . GOLD ) ) ;
2021-06-10 17:25:05 +00:00
} else {
if ( this . claimName . isEmpty ( ) )
2022-01-30 17:31:45 +00:00
l . add ( PermHelper . simpleColoredText ( String . format ( ConfigHandler . langManager . get ( " claimBasicInfoSub " ) , ownerName , this . minX , this . minZ , this . maxX , this . maxZ ) , ChatFormatting . GOLD ) ) ;
2021-06-10 17:25:05 +00:00
else
2022-01-30 17:31:45 +00:00
l . add ( PermHelper . simpleColoredText ( String . format ( ConfigHandler . langManager . get ( " claimBasicInfoSubNamed " ) , ownerName , this . minX , this . minZ , this . maxX , this . maxZ , this . claimName ) , ChatFormatting . GOLD ) ) ;
2021-06-10 17:25:05 +00:00
}
2020-08-25 17:43:52 +00:00
if ( perms ) {
2021-07-03 18:26:56 +00:00
if ( infoType = = InfoType . ALL | | infoType = = InfoType . GLOBAL )
2022-07-01 13:19:50 +00:00
l . add ( fromPermissionMap ( " claimInfoPerms " , this . globalPerm ) ) ;
2021-07-03 18:26:56 +00:00
if ( infoType = = InfoType . ALL | | infoType = = InfoType . GROUP ) {
2022-07-01 13:19:50 +00:00
l . add ( PermHelper . simpleColoredText ( ConfigHandler . langManager . get ( " claimGroupInfoHeader " ) , ChatFormatting . GOLD ) ) ;
2021-07-03 18:26:56 +00:00
Map < String , List < String > > nameToGroup = new HashMap < > ( ) ;
for ( Map . Entry < UUID , String > e : this . playersGroups . entrySet ( ) ) {
2021-12-03 21:37:35 +00:00
player . getServer ( ) . getProfileCache ( ) . get ( e . getKey ( ) ) . ifPresent ( pgroup - >
nameToGroup . merge ( e . getValue ( ) , Lists . newArrayList ( pgroup . getName ( ) ) , ( old , val ) - > {
old . add ( pgroup . getName ( ) ) ;
return old ;
} )
) ;
2021-07-03 18:26:56 +00:00
}
for ( Map . Entry < String , Map < ClaimPermission , Boolean > > e : this . permissions . entrySet ( ) ) {
2022-07-01 13:19:50 +00:00
l . add ( PermHelper . simpleColoredText ( String . format ( " %s: " , e . getKey ( ) ) , ChatFormatting . YELLOW ) ) ;
l . add ( fromPermissionMap ( " claimGroupPerms " , e . getValue ( ) ) ) ;
2022-01-30 17:31:45 +00:00
l . add ( PermHelper . simpleColoredText ( String . format ( ConfigHandler . langManager . get ( " claimGroupPlayers " ) , nameToGroup . getOrDefault ( e . getKey ( ) , new ArrayList < > ( ) ) ) , ChatFormatting . RED ) ) ;
2020-08-23 21:18:18 +00:00
}
}
}
2021-12-03 21:37:35 +00:00
l . add ( PermHelper . simpleColoredText ( " ============================================= " , ChatFormatting . GREEN ) ) ;
2020-08-23 21:18:18 +00:00
return l ;
2020-08-23 12:52:36 +00:00
}
2021-07-03 18:26:56 +00:00
2022-07-01 13:19:50 +00:00
private static Component fromPermissionMap ( String lang , Map < ClaimPermission , Boolean > map ) {
2022-07-01 16:26:13 +00:00
MutableComponent mapComp = Component . literal ( " [ " ) . withStyle ( ChatFormatting . GRAY ) ;
2022-07-01 13:19:50 +00:00
int i = 0 ;
for ( Map . Entry < ClaimPermission , Boolean > entry : map . entrySet ( ) ) {
2022-07-01 16:26:13 +00:00
MutableComponent pComp = Component . literal ( ( i ! = 0 ? " , " : " " ) + entry . getKey ( ) . id + " = " ) . withStyle ( ChatFormatting . GRAY ) ;
pComp . append ( Component . literal ( entry . getValue ( ) . toString ( ) ) . withStyle ( entry . getValue ( ) ? ChatFormatting . GREEN : ChatFormatting . RED ) ) ;
2022-07-01 13:19:50 +00:00
mapComp . append ( pComp ) ;
i + + ;
}
mapComp . append ( " ] " ) ;
2022-07-01 16:26:13 +00:00
MutableComponent component = Component . translatable ( ConfigHandler . langManager . get ( lang ) , mapComp ) . withStyle ( ChatFormatting . DARK_BLUE ) ;
2022-07-01 13:19:50 +00:00
return component ;
}
2022-11-26 19:33:32 +00:00
public DisplayBox display ( ) {
2022-12-18 19:37:36 +00:00
return new ClaimDisplayBox ( this , ( ) - > new DisplayBox . Box ( this . minX , this . minY , this . minZ , this . maxX , this . world . getMaxBuildHeight ( ) , this . maxZ ) , this : : isRemoved ) ;
2022-11-26 19:33:32 +00:00
}
2021-07-03 18:26:56 +00:00
public enum InfoType {
ALL ,
SIMPLE ,
GLOBAL ,
GROUP
}
2021-07-31 15:56:43 +00:00
interface ClaimUpdater {
2022-01-08 13:53:13 +00:00
Map < Integer , ClaimUpdater > updater = Config . createHashMap ( map - > map . put ( 2 , claim - > claim . globalPerm . put ( PermissionRegistry . LOCKITEMS , true ) ) ) ;
2021-07-31 15:56:43 +00:00
static void updateClaim ( Claim claim ) {
updater . entrySet ( ) . stream ( ) . filter ( e - > e . getKey ( ) > ConfigHandler . config . preConfigVersion ) . map ( Map . Entry : : getValue )
. forEach ( up - > up . update ( claim ) ) ;
}
void update ( Claim claim ) ;
}
2020-08-23 12:52:36 +00:00
}