add other potential damage sources to checks

This commit is contained in:
Flemmli97 2020-08-24 23:18:52 +02:00
parent 327d714dd6
commit b5254cf8e1
4 changed files with 78 additions and 15 deletions

View File

@ -6,6 +6,8 @@ import com.flemmli97.flan.claim.EnumPermission;
import com.flemmli97.flan.claim.BlockToPermissionMap;
import com.flemmli97.flan.claim.PermHelper;
import com.flemmli97.flan.config.ConfigHandler;
import com.flemmli97.flan.player.EnumDisplayType;
import com.flemmli97.flan.player.PlayerClaimData;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.LockableContainerBlockEntity;
@ -35,8 +37,10 @@ public class BlockInteractEvents {
ClaimStorage storage = ClaimStorage.get((ServerWorld) world);
Claim claim = storage.getClaimAt(pos);
if (claim != null) {
if (!claim.canInteract(player, EnumPermission.BREAK, pos, true))
if (!claim.canInteract(player, EnumPermission.BREAK, pos, true)) {
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
return ActionResult.SUCCESS;
}
}
return ActionResult.PASS;
}
@ -64,15 +68,29 @@ public class BlockInteractEvents {
BlockState state = world.getBlockState(hitResult.getBlockPos());
BlockEntity blockEntity = world.getBlockEntity(hitResult.getBlockPos());
if (blockEntity != null) {
if (blockEntity instanceof LockableContainerBlockEntity)
return claim.canInteract(player, EnumPermission.OPENCONTAINER, hitResult.getBlockPos(), true) ? ActionResult.PASS : ActionResult.FAIL;
if (blockEntity instanceof LockableContainerBlockEntity) {
if(claim.canInteract(player, EnumPermission.OPENCONTAINER, hitResult.getBlockPos(), true))
return ActionResult.PASS;
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
return ActionResult.FAIL;
}
}
EnumPermission perm = BlockToPermissionMap.getFromBlock(state.getBlock());
if (perm != null)
return claim.canInteract(player, perm, hitResult.getBlockPos(), true) ? ActionResult.PASS : ActionResult.FAIL;
if (perm != null) {
if(claim.canInteract(player, perm, hitResult.getBlockPos(), true))
return ActionResult.PASS;
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
return ActionResult.FAIL;
}
}
BlockPos placePos = hitResult.getBlockPos().offset(hitResult.getSide());
claim = storage.getClaimAt(placePos);
if (stack.getItem() instanceof BlockItem || stack.getItem() instanceof ToolItem || stack.getItem() == Items.ARMOR_STAND) {
if(claim.canInteract(player, EnumPermission.PLACE, placePos, true))
return ActionResult.PASS;
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
return ActionResult.FAIL;
}
if (stack.getItem() instanceof BlockItem || stack.getItem() instanceof ToolItem || stack.getItem() == Items.ARMOR_STAND)
return claim.canInteract(player, EnumPermission.PLACE, hitResult.getBlockPos(), true) ? ActionResult.PASS : ActionResult.FAIL;
}
return ActionResult.PASS;
}

View File

@ -7,14 +7,19 @@ import com.flemmli97.flan.claim.BlockToPermissionMap;
import com.flemmli97.flan.mixin.IPersistentProjectileVars;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.boss.WitherEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.EntityDamageSource;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.entity.decoration.ItemFrameEntity;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.entity.projectile.TridentEntity;
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.entity.vehicle.BoatEntity;
@ -37,7 +42,7 @@ import net.minecraft.world.World;
public class EntityInteractEvents {
public static ActionResult attackEntity(PlayerEntity player, World world, Hand hand, Entity entity, EntityHitResult hitResult) {
return attackSimple(player, entity);
return attackSimple(player, entity, true);
}
public static ActionResult useAtEntity(PlayerEntity player, World world, Hand hand, Entity entity, /* Nullable */ EntityHitResult hitResult) {
@ -121,13 +126,30 @@ public class EntityInteractEvents {
//player.getServer().send(new ServerTask(player.getServer().getTicks()+2, ()->player.world.updateListeners(pos, state, state, 2)));
}
return flag;
} else if (res.getType() == HitResult.Type.ENTITY)
return attackSimple(player, ((EntityHitResult) res).getEntity()) != ActionResult.PASS;
} else if (res.getType() == HitResult.Type.ENTITY){
if(proj instanceof EnderPearlEntity) {
ClaimStorage storage = ClaimStorage.get((ServerWorld) proj.world);
Claim claim = storage.getClaimAt(proj.getBlockPos());
return claim.canInteract(player, EnumPermission.ENDERPEARL, proj.getBlockPos(), true);
}
return attackSimple(player, ((EntityHitResult) res).getEntity(), true) != ActionResult.PASS;
}
}
return false;
}
public static ActionResult attackSimple(PlayerEntity p, Entity entity) {
public static boolean hurtEntity(LivingEntity entity, DamageSource source){
if(source.getAttacker() instanceof ServerPlayerEntity)
return attackSimple((ServerPlayerEntity) source.getAttacker(), entity, false)!=ActionResult.PASS;
else if(source.isExplosive() && !entity.world.isClient){
Claim claim = ClaimStorage.get((ServerWorld) entity.world).getClaimAt(entity.getBlockPos());
if(claim!=null && !claim.canInteract(null, EnumPermission.EXPLOSIONS, entity.getBlockPos()))
return true;
}
return false;
}
public static ActionResult attackSimple(PlayerEntity p, Entity entity, boolean message) {
if (p.world.isClient)
return ActionResult.PASS;
if (entity instanceof Monster)
@ -138,10 +160,10 @@ public class EntityInteractEvents {
Claim claim = storage.getClaimAt(pos);
if (claim != null) {
if (entity instanceof ArmorStandEntity || entity instanceof MinecartEntity || entity instanceof BoatEntity || entity instanceof ItemFrameEntity)
return claim.canInteract(player, EnumPermission.BREAKNONLIVING, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
return claim.canInteract(player, EnumPermission.BREAKNONLIVING, pos, message) ? ActionResult.PASS : ActionResult.FAIL;
if (entity instanceof PlayerEntity)
return claim.canInteract(player, EnumPermission.HURTPLAYER, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
return claim.canInteract(player, EnumPermission.HURTANIMAL, pos, true) ? ActionResult.PASS : ActionResult.FAIL;
return claim.canInteract(player, EnumPermission.HURTPLAYER, pos, message) ? ActionResult.PASS : ActionResult.FAIL;
return claim.canInteract(player, EnumPermission.HURTANIMAL, pos, message) ? ActionResult.PASS : ActionResult.FAIL;
}
return ActionResult.PASS;
}

View File

@ -0,0 +1,22 @@
package com.flemmli97.flan.mixin;
import com.flemmli97.flan.event.EntityInteractEvents;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(LivingEntity.class)
public class LivingEntityMixin {
@Inject(method = "damage", at = @At(value = "HEAD"), cancellable = true)
public void onDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info){
if(EntityInteractEvents.hurtEntity((LivingEntity)(Object)this, source)) {
info.setReturnValue(false);
info.cancel();
}
}
}

View File

@ -15,7 +15,8 @@
"WitherMixin",
"ExplosionMixin",
"EnderPearlEntityMixin",
"IPersistentProjectileVars"
"IPersistentProjectileVars",
"LivingEntityMixin"
],
"server": [
],