2020-08-23 08:52:36 -04:00
|
|
|
package com.flemmli97.flan.event;
|
|
|
|
|
2020-11-28 14:53:29 -05:00
|
|
|
import com.flemmli97.flan.api.ClaimPermission;
|
|
|
|
import com.flemmli97.flan.api.PermissionRegistry;
|
2021-05-12 07:29:29 -04:00
|
|
|
import com.flemmli97.flan.claim.Claim;
|
2020-08-23 08:52:36 -04:00
|
|
|
import com.flemmli97.flan.claim.ClaimStorage;
|
2020-10-30 10:09:57 -04:00
|
|
|
import com.flemmli97.flan.claim.IPermissionContainer;
|
2021-04-11 17:34:45 -04:00
|
|
|
import com.flemmli97.flan.claim.ObjectToPermissionMap;
|
2020-08-23 08:52:36 -04:00
|
|
|
import com.flemmli97.flan.mixin.IPersistentProjectileVars;
|
2021-04-11 19:50:31 -04:00
|
|
|
import com.flemmli97.flan.player.IOwnedItem;
|
2021-03-10 14:04:55 -05:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.entity.Entity;
|
2021-04-11 19:50:31 -04:00
|
|
|
import net.minecraft.entity.ItemEntity;
|
2020-08-24 17:18:52 -04:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.entity.boss.WitherEntity;
|
2020-08-24 17:18:52 -04:00
|
|
|
import net.minecraft.entity.damage.DamageSource;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.entity.decoration.ArmorStandEntity;
|
|
|
|
import net.minecraft.entity.decoration.ItemFrameEntity;
|
2021-05-12 13:58:24 -04:00
|
|
|
import net.minecraft.entity.mob.EndermanEntity;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.entity.mob.Monster;
|
2021-05-12 13:58:24 -04:00
|
|
|
import net.minecraft.entity.passive.SnowGolemEntity;
|
2020-09-18 16:29:11 -04:00
|
|
|
import net.minecraft.entity.passive.TameableEntity;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.entity.passive.VillagerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.projectile.PersistentProjectileEntity;
|
|
|
|
import net.minecraft.entity.projectile.ProjectileEntity;
|
2021-04-11 17:34:45 -04:00
|
|
|
import net.minecraft.entity.projectile.thrown.EggEntity;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
|
2021-04-11 17:34:45 -04:00
|
|
|
import net.minecraft.entity.projectile.thrown.PotionEntity;
|
2020-08-23 09:16:26 -04:00
|
|
|
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.entity.vehicle.BoatEntity;
|
2020-08-23 09:16:26 -04:00
|
|
|
import net.minecraft.entity.vehicle.StorageMinecartEntity;
|
2021-04-11 19:50:31 -04:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.network.packet.s2c.play.InventoryS2CPacket;
|
2021-06-08 15:01:38 -04:00
|
|
|
import net.minecraft.network.packet.s2c.play.PlayerAbilitiesS2CPacket;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.sound.SoundEvents;
|
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.Hand;
|
2021-05-12 07:29:29 -04:00
|
|
|
import net.minecraft.util.Pair;
|
2021-04-11 19:50:31 -04:00
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.hit.EntityHitResult;
|
|
|
|
import net.minecraft.util.hit.HitResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-05-12 07:29:29 -04:00
|
|
|
import net.minecraft.util.math.Direction;
|
2021-06-08 15:01:38 -04:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2020-08-23 08:52:36 -04:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
|
|
|
import net.minecraft.world.World;
|
2021-05-12 07:29:29 -04:00
|
|
|
|
|
|
|
import java.util.function.Consumer;
|
2020-08-23 08:52:36 -04:00
|
|
|
|
|
|
|
public class EntityInteractEvents {
|
|
|
|
|
|
|
|
public static ActionResult attackEntity(PlayerEntity player, World world, Hand hand, Entity entity, EntityHitResult hitResult) {
|
2020-08-24 17:18:52 -04:00
|
|
|
return attackSimple(player, entity, true);
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ActionResult useAtEntity(PlayerEntity player, World world, Hand hand, Entity entity, /* Nullable */ EntityHitResult hitResult) {
|
2020-08-24 19:20:22 -04:00
|
|
|
if (player.world.isClient || player.isSpectator())
|
2020-08-23 08:52:36 -04:00
|
|
|
return ActionResult.PASS;
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) world);
|
|
|
|
BlockPos pos = entity.getBlockPos();
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2020-08-23 08:52:36 -04:00
|
|
|
if (claim != null) {
|
|
|
|
if (entity instanceof ArmorStandEntity) {
|
2020-11-28 14:53:29 -05:00
|
|
|
if (!claim.canInteract((ServerPlayerEntity) player, PermissionRegistry.ARMORSTAND, pos, true))
|
2020-08-23 08:52:36 -04:00
|
|
|
return ActionResult.FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
2020-08-23 17:18:18 -04:00
|
|
|
public static ActionResult useEntity(PlayerEntity p, World world, Hand hand, Entity entity) {
|
2020-08-25 13:43:52 -04:00
|
|
|
if (p.world.isClient || p.isSpectator())
|
2020-08-23 17:18:18 -04:00
|
|
|
return ActionResult.PASS;
|
|
|
|
ServerPlayerEntity player = (ServerPlayerEntity) p;
|
2020-08-23 08:52:36 -04:00
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) world);
|
|
|
|
BlockPos pos = entity.getBlockPos();
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2020-08-23 08:52:36 -04:00
|
|
|
if (claim != null) {
|
|
|
|
if (entity instanceof BoatEntity)
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.BOAT, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
|
2020-08-23 09:16:26 -04:00
|
|
|
if (entity instanceof AbstractMinecartEntity) {
|
|
|
|
if (entity instanceof StorageMinecartEntity)
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.OPENCONTAINER, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
|
|
|
|
return claim.canInteract(player, PermissionRegistry.MINECART, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
if (entity instanceof VillagerEntity)
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.TRADING, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
|
2020-08-23 08:52:36 -04:00
|
|
|
if (entity instanceof ItemFrameEntity)
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.ITEMFRAMEROTATE, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
|
|
|
|
if (entity instanceof TameableEntity) {
|
2020-09-18 16:29:11 -04:00
|
|
|
TameableEntity tame = (TameableEntity) entity;
|
2020-11-28 14:53:29 -05:00
|
|
|
if (tame.isOwner(player))
|
2020-09-18 16:29:11 -04:00
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.ANIMALINTERACT, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean projectileHit(ProjectileEntity proj, HitResult res) {
|
|
|
|
if (proj.world.isClient)
|
|
|
|
return false;
|
|
|
|
Entity owner = proj.getOwner();
|
2020-08-23 17:18:18 -04:00
|
|
|
if (owner instanceof ServerPlayerEntity) {
|
|
|
|
ServerPlayerEntity player = (ServerPlayerEntity) owner;
|
2020-08-23 08:52:36 -04:00
|
|
|
if (res.getType() == HitResult.Type.BLOCK) {
|
|
|
|
BlockHitResult blockRes = (BlockHitResult) res;
|
|
|
|
BlockPos pos = blockRes.getBlockPos();
|
|
|
|
BlockState state = proj.world.getBlockState(pos);
|
2021-04-11 17:34:45 -04:00
|
|
|
ClaimPermission perm;
|
2020-08-23 08:52:36 -04:00
|
|
|
if (proj instanceof EnderPearlEntity)
|
2020-11-28 14:53:29 -05:00
|
|
|
perm = PermissionRegistry.ENDERPEARL;
|
2021-04-11 17:34:45 -04:00
|
|
|
else if (proj instanceof EggEntity || proj instanceof PotionEntity)
|
|
|
|
perm = PermissionRegistry.PROJECTILES;
|
|
|
|
else
|
|
|
|
perm = ObjectToPermissionMap.getFromBlock(state.getBlock());
|
2020-11-28 14:53:29 -05:00
|
|
|
if (perm != PermissionRegistry.ENDERPEARL && perm != PermissionRegistry.TARGETBLOCK && perm != PermissionRegistry.PROJECTILES)
|
2020-08-23 08:52:36 -04:00
|
|
|
return false;
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) proj.world);
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2020-08-23 08:52:36 -04:00
|
|
|
if (claim == null)
|
|
|
|
return false;
|
2020-08-24 15:03:06 -04:00
|
|
|
boolean flag = !claim.canInteract(player, perm, pos, true);
|
2020-08-23 17:18:18 -04:00
|
|
|
if (flag) {
|
2020-08-25 13:43:52 -04:00
|
|
|
if (proj instanceof PersistentProjectileEntity) {
|
2020-08-23 17:18:18 -04:00
|
|
|
PersistentProjectileEntity pers = (PersistentProjectileEntity) proj;
|
|
|
|
((IPersistentProjectileVars) pers).setInBlockState(pers.world.getBlockState(pos));
|
|
|
|
Vec3d vec3d = blockRes.getPos().subtract(pers.getX(), pers.getY(), pers.getZ());
|
|
|
|
pers.setVelocity(vec3d);
|
|
|
|
Vec3d vec3d2 = vec3d.normalize().multiply(0.05000000074505806D);
|
|
|
|
pers.setPos(pers.getX() - vec3d2.x, pers.getY() - vec3d2.y, pers.getZ() - vec3d2.z);
|
|
|
|
pers.playSound(((IPersistentProjectileVars) pers).getSoundEvent(), 1.0F, 1.2F / (pers.world.random.nextFloat() * 0.2F + 0.9F));
|
|
|
|
((IPersistentProjectileVars) pers).setInGround(true);
|
|
|
|
pers.shake = 7;
|
|
|
|
pers.setCritical(false);
|
|
|
|
pers.setPierceLevel((byte) 0);
|
|
|
|
pers.setSound(SoundEvents.ENTITY_ARROW_HIT);
|
|
|
|
pers.setShotFromCrossbow(false);
|
|
|
|
((IPersistentProjectileVars) pers).resetPiercingStatus();
|
|
|
|
}
|
2021-03-10 14:04:55 -05:00
|
|
|
//TODO: find a way to properly update chorus fruit break on hit
|
2020-08-23 17:18:18 -04:00
|
|
|
//player.getServer().send(new ServerTask(player.getServer().getTicks()+2, ()->player.world.updateListeners(pos, state, state, 2)));
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
return flag;
|
2020-08-25 13:43:52 -04:00
|
|
|
} else if (res.getType() == HitResult.Type.ENTITY) {
|
|
|
|
if (proj instanceof EnderPearlEntity) {
|
2020-08-24 17:18:52 -04:00
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) proj.world);
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(proj.getBlockPos());
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.ENDERPEARL, proj.getBlockPos(), true);
|
2020-08-24 17:18:52 -04:00
|
|
|
}
|
2021-03-10 14:04:55 -05:00
|
|
|
Entity hit = ((EntityHitResult) res).getEntity();
|
|
|
|
boolean fail = attackSimple(player, hit, true) != ActionResult.PASS;
|
2021-04-04 06:33:10 -04:00
|
|
|
if (fail && proj instanceof PersistentProjectileEntity && ((PersistentProjectileEntity) proj).getPierceLevel() > 0) {
|
2021-03-10 14:04:55 -05:00
|
|
|
PersistentProjectileEntity pers = (PersistentProjectileEntity) proj;
|
|
|
|
IntOpenHashSet pierced = ((IPersistentProjectileVars) pers).getPiercedEntities();
|
2021-04-04 06:33:10 -04:00
|
|
|
if (pierced == null)
|
2021-03-10 14:04:55 -05:00
|
|
|
pierced = new IntOpenHashSet(5);
|
|
|
|
pierced.add(hit.getEntityId());
|
|
|
|
((IPersistentProjectileVars) pers).setPiercedEntities(pierced);
|
2021-04-04 06:33:10 -04:00
|
|
|
pers.setPierceLevel((byte) (pers.getPierceLevel() + 1));
|
2021-03-10 14:04:55 -05:00
|
|
|
}
|
|
|
|
return fail;
|
2020-08-24 17:18:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-31 08:22:18 -04:00
|
|
|
public static boolean preventDamage(Entity entity, DamageSource source) {
|
2020-08-25 13:43:52 -04:00
|
|
|
if (source.getAttacker() instanceof ServerPlayerEntity)
|
2021-05-31 08:22:18 -04:00
|
|
|
return attackSimple((ServerPlayerEntity) source.getAttacker(), entity, true) != ActionResult.PASS;
|
2020-08-25 13:43:52 -04:00
|
|
|
else if (source.isExplosive() && !entity.world.isClient) {
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = ClaimStorage.get((ServerWorld) entity.world).getForPermissionCheck(entity.getBlockPos());
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim != null && !claim.canInteract(null, PermissionRegistry.EXPLOSIONS, entity.getBlockPos());
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:18:52 -04:00
|
|
|
public static ActionResult attackSimple(PlayerEntity p, Entity entity, boolean message) {
|
2020-08-24 19:20:22 -04:00
|
|
|
if (p.world.isClient || p.isSpectator())
|
2020-08-23 08:52:36 -04:00
|
|
|
return ActionResult.PASS;
|
|
|
|
if (entity instanceof Monster)
|
|
|
|
return ActionResult.PASS;
|
2020-08-23 17:18:18 -04:00
|
|
|
ServerPlayerEntity player = (ServerPlayerEntity) p;
|
|
|
|
ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
|
2020-08-23 08:52:36 -04:00
|
|
|
BlockPos pos = entity.getBlockPos();
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2020-08-23 08:52:36 -04:00
|
|
|
if (claim != null) {
|
2021-05-31 08:22:18 -04:00
|
|
|
if (!(entity instanceof LivingEntity))
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.BREAKNONLIVING, pos, message) ? ActionResult.PASS : ActionResult.FAIL;
|
2020-08-23 08:52:36 -04:00
|
|
|
if (entity instanceof PlayerEntity)
|
2020-11-28 14:53:29 -05:00
|
|
|
return claim.canInteract(player, PermissionRegistry.HURTPLAYER, pos, message) ? ActionResult.PASS : ActionResult.FAIL;
|
|
|
|
return claim.canInteract(player, PermissionRegistry.HURTANIMAL, pos, message) ? ActionResult.PASS : ActionResult.FAIL;
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean xpAbsorb(PlayerEntity player) {
|
|
|
|
if (player instanceof ServerPlayerEntity) {
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) player.world);
|
|
|
|
BlockPos pos = player.getBlockPos();
|
2020-10-30 10:09:57 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2020-08-23 08:52:36 -04:00
|
|
|
if (claim != null)
|
2020-11-28 14:53:29 -05:00
|
|
|
return !claim.canInteract((ServerPlayerEntity) player, PermissionRegistry.XP, pos, false);
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:50:31 -04:00
|
|
|
public static boolean canCollideWith(PlayerEntity player, Entity entity) {
|
|
|
|
if (player instanceof ServerPlayerEntity) {
|
|
|
|
if (entity instanceof ItemEntity) {
|
|
|
|
if (player.getUuid().equals(((IOwnedItem) entity).getPlayerOrigin()))
|
|
|
|
return true;
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) player.world);
|
|
|
|
BlockPos pos = player.getBlockPos();
|
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
|
|
|
if (claim != null)
|
|
|
|
return claim.canInteract((ServerPlayerEntity) player, PermissionRegistry.PICKUP, pos, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canDropItem(PlayerEntity player, ItemStack stack) {
|
|
|
|
if (!player.isDead() && player instanceof ServerPlayerEntity) {
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) player.world);
|
|
|
|
BlockPos pos = player.getBlockPos();
|
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
|
|
|
boolean allow = true;
|
|
|
|
if (claim != null)
|
|
|
|
allow = claim.canInteract((ServerPlayerEntity) player, PermissionRegistry.DROP, pos, false);
|
|
|
|
if (!allow) {
|
|
|
|
player.inventory.insertStack(stack);
|
|
|
|
DefaultedList<ItemStack> stacks = DefaultedList.of();
|
|
|
|
for (int j = 0; j < player.currentScreenHandler.slots.size(); ++j) {
|
|
|
|
ItemStack itemStack2 = player.currentScreenHandler.slots.get(j).getStack();
|
|
|
|
stacks.add(itemStack2.isEmpty() ? ItemStack.EMPTY : itemStack2);
|
|
|
|
}
|
|
|
|
((ServerPlayerEntity) player).networkHandler.sendPacket(new InventoryS2CPacket(player.currentScreenHandler.syncId, stacks));
|
|
|
|
}
|
|
|
|
return allow;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-23 08:52:36 -04:00
|
|
|
public static boolean witherCanDestroy(WitherEntity wither) {
|
|
|
|
if (wither.world.isClient)
|
|
|
|
return true;
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) wither.world);
|
2021-04-19 07:48:47 -04:00
|
|
|
BlockPos.Mutable pos = wither.getBlockPos().mutableCopy();
|
2020-08-23 08:52:36 -04:00
|
|
|
for (int x = -1; x <= 1; x++)
|
|
|
|
for (int z = -1; z <= 1; z++) {
|
2021-04-19 07:48:47 -04:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(wither.getBlockPos().add(x, 0, z));
|
2021-05-12 14:24:14 -04:00
|
|
|
if (!claim.canInteract(null, PermissionRegistry.WITHER, pos.set(pos.getX() + x, pos.getY() + 3, pos.getZ() + z), false))
|
2020-08-23 08:52:36 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-12 13:58:24 -04:00
|
|
|
|
|
|
|
public static boolean canEndermanInteract(EndermanEntity enderman, BlockPos pos) {
|
|
|
|
if (enderman.world.isClient)
|
|
|
|
return true;
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) enderman.world);
|
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2021-05-12 14:24:14 -04:00
|
|
|
return claim.canInteract(null, PermissionRegistry.ENDERMAN, pos, false);
|
2021-05-12 13:58:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canSnowGolemInteract(SnowGolemEntity snowgolem) {
|
|
|
|
if (snowgolem.world.isClient)
|
|
|
|
return true;
|
|
|
|
int x, y, z;
|
|
|
|
for (int l = 0; l < 4; ++l) {
|
|
|
|
x = MathHelper.floor(snowgolem.getX() + (l % 2 * 2 - 1) * 0.25F);
|
|
|
|
y = MathHelper.floor(snowgolem.getY());
|
|
|
|
z = MathHelper.floor(snowgolem.getZ() + (l / 2 % 2 * 2 - 1) * 0.25F);
|
|
|
|
BlockPos pos = new BlockPos(x, y, z);
|
|
|
|
IPermissionContainer claim = ClaimStorage.get((ServerWorld) snowgolem.world).getForPermissionCheck(pos);
|
|
|
|
if (!claim.canInteract(null, PermissionRegistry.SNOWGOLEM, pos, false))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-08 14:09:30 -04:00
|
|
|
|
2021-06-08 15:01:38 -04:00
|
|
|
public static void updateClaim(ServerPlayerEntity player, Claim currentClaim, Consumer<Claim> cons) {
|
|
|
|
Vec3d pos = player.getPos();
|
|
|
|
BlockPos rounded = roundedBlockPos(pos.add(0, player.getEyeHeight(player.getPose()), 0));
|
|
|
|
ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
|
|
|
|
if (currentClaim != null) {
|
|
|
|
if (!currentClaim.insideClaim(rounded)) {
|
|
|
|
cons.accept(null);
|
|
|
|
} else {
|
|
|
|
if (!player.isSpectator()) {
|
|
|
|
BlockPos.Mutable bPos = rounded.mutableCopy();
|
|
|
|
if (!currentClaim.canInteract(player, PermissionRegistry.CANSTAY, bPos, true)) {
|
|
|
|
Vec3d tp = getTeleportPos(player, pos, storage, currentClaim.getDimensions(), bPos);
|
|
|
|
player.teleport(tp.getX(), tp.getY(), tp.getZ());
|
|
|
|
}
|
|
|
|
if (player.abilities.flying && !currentClaim.canInteract(player, PermissionRegistry.FLIGHT, rounded, true)) {
|
|
|
|
player.abilities.flying = false;
|
|
|
|
player.networkHandler.sendPacket(new PlayerAbilitiesS2CPacket(player.abilities));
|
|
|
|
}
|
2021-05-12 07:29:29 -04:00
|
|
|
}
|
|
|
|
}
|
2021-06-08 15:01:38 -04:00
|
|
|
} else if (player.age % 3 == 0) {
|
|
|
|
Claim claim = storage.getClaimAt(rounded);
|
|
|
|
cons.accept(claim);
|
2021-05-12 07:29:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:01:38 -04:00
|
|
|
private static BlockPos roundedBlockPos(Vec3d pos) {
|
|
|
|
return new BlockPos(Math.round(pos.getX()), MathHelper.floor(pos.getY()), Math.round(pos.getZ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Vec3d getTeleportPos(ServerPlayerEntity player, Vec3d playerPos, ClaimStorage storage, int[] dim, BlockPos.Mutable bPos) {
|
2021-05-12 07:29:29 -04:00
|
|
|
Pair<Direction, Vec3d> pos = nearestOutside(dim, playerPos);
|
|
|
|
bPos.set(pos.getRight().getX(), pos.getRight().getY(), pos.getRight().getZ());
|
|
|
|
Claim claim = storage.getClaimAt(bPos);
|
2021-06-08 15:01:38 -04:00
|
|
|
if (claim == null || claim.canInteract(player, PermissionRegistry.CANSTAY, bPos, false))
|
2021-05-12 07:29:29 -04:00
|
|
|
return pos.getRight();
|
|
|
|
int[] newDim = claim.getDimensions();
|
2021-06-08 15:01:38 -04:00
|
|
|
switch (pos.getLeft()) {
|
2021-05-12 07:29:29 -04:00
|
|
|
case NORTH:
|
|
|
|
dim[2] = newDim[2];
|
|
|
|
break;
|
|
|
|
case SOUTH:
|
|
|
|
dim[3] = newDim[3];
|
|
|
|
break;
|
|
|
|
case EAST:
|
|
|
|
dim[1] = newDim[1];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dim[0] = newDim[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return getTeleportPos(player, playerPos, storage, dim, bPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static Pair<Direction, Vec3d> nearestOutside(int[] dim, Vec3d from) {
|
|
|
|
double northDist = Math.abs(from.getZ() - dim[2]);
|
|
|
|
double southDist = Math.abs(dim[3] - from.getZ());
|
|
|
|
double westDist = Math.abs(from.getX() - dim[0]);
|
|
|
|
double eastDist = Math.abs(dim[1] - from.getX());
|
2021-06-08 15:01:38 -04:00
|
|
|
if (northDist > southDist) {
|
|
|
|
if (eastDist > westDist) {
|
|
|
|
if (southDist > westDist)
|
2021-05-12 07:29:29 -04:00
|
|
|
return new Pair<>(Direction.WEST, new Vec3d(dim[0] - 1.5, from.getY(), from.getZ()));
|
|
|
|
return new Pair<>(Direction.SOUTH, new Vec3d(from.getX(), from.getY(), dim[3] + 1.5));
|
|
|
|
}
|
2021-06-08 15:01:38 -04:00
|
|
|
if (southDist > eastDist)
|
2021-05-12 07:29:29 -04:00
|
|
|
return new Pair<>(Direction.EAST, new Vec3d(dim[1] + 1.5, from.getY(), from.getZ()));
|
2021-06-08 15:01:38 -04:00
|
|
|
return new Pair<>(Direction.SOUTH, new Vec3d(from.getX(), from.getY(), dim[3] + 1.5));
|
2021-05-12 07:29:29 -04:00
|
|
|
}
|
2021-06-08 15:01:38 -04:00
|
|
|
if (eastDist > westDist) {
|
|
|
|
if (northDist > westDist)
|
2021-05-12 07:29:29 -04:00
|
|
|
return new Pair<>(Direction.WEST, new Vec3d(dim[0] - 1.5, from.getY(), from.getZ()));
|
|
|
|
return new Pair<>(Direction.NORTH, new Vec3d(from.getX(), from.getY(), dim[2] - 1.5));
|
|
|
|
}
|
2021-06-08 15:01:38 -04:00
|
|
|
if (northDist > eastDist)
|
2021-05-12 07:29:29 -04:00
|
|
|
return new Pair<>(Direction.EAST, new Vec3d(dim[1] + 1.5, from.getY(), from.getZ()));
|
2021-06-08 15:01:38 -04:00
|
|
|
return new Pair<>(Direction.NORTH, new Vec3d(from.getX(), from.getY(), dim[2] - 1.5));
|
2021-05-12 07:29:29 -04:00
|
|
|
}
|
2020-08-23 08:52:36 -04:00
|
|
|
}
|