Block protection compat overhaul

- Unifies all block protection checks into helper methods in BlockUtils which trigger all the necessary forge events and vanilla methods
- Implements these checks in all the appropriate places and removes calls to EntityUtils#canDamageBlocks(...) where they are no longer needed
- Changes the mine spell to respect the config settings in line with the rest of the spells
- Fixes earthquake not respecting the settings at all
- Adds a separate config option for dispenser block damage and updates the description for the existing settings to reflect the changes
This commit is contained in:
Electroblob77
2020-09-23 00:57:55 +01:00
parent f49ef7594d
commit 2f91573284
22 changed files with 238 additions and 86 deletions
@@ -72,7 +72,7 @@ public class FireBreath extends SpellRay {
pos = pos.offset(side);
if(world.isAirBlock(pos)){
if(!world.isRemote) world.setBlockState(pos, Blocks.FIRE.getDefaultState());
if(!world.isRemote && BlockUtils.canPlaceBlock(caster, world, pos)) world.setBlockState(pos, Blocks.FIRE.getDefaultState());
return true;
}
@@ -97,7 +97,8 @@ public class Firestorm extends SpellAreaEffect {
double dist = origin.distanceTo(new Vec3d(origin.x + i, y, origin.z + j));
// Randomised with weighting so that the nearer the block the more likely it is to be set alight.
if(y != -1 && world.rand.nextInt((int)(dist * 2) + 1) < radius && dist < radius && dist > 1.5){
if(y != -1 && world.rand.nextInt((int)(dist * 2) + 1) < radius && dist < radius && dist > 1.5
&& BlockUtils.canPlaceBlock(caster, world, pos)){
world.setBlockState(pos, Blocks.FIRE.getDefaultState());
}
}
@@ -58,7 +58,7 @@ public class Freeze extends SpellRay {
@Override
protected boolean onBlockHit(World world, BlockPos pos, EnumFacing side, Vec3d hit, EntityLivingBase caster, Vec3d origin, int ticksInUse, SpellModifiers modifiers){
if(!world.isRemote && EntityUtils.canDamageBlocks(caster, world)){
if(!world.isRemote && BlockUtils.canPlaceBlock(caster, world, pos)){
BlockUtils.freeze(world, pos, true);
}
@@ -54,7 +54,7 @@ public class IceAge extends SpellAreaEffect {
if(target instanceof EntityLiving){
if(((BlockStatue)WizardryBlocks.ice_statue).convertToStatue((EntityLiving)target,
(int)(getProperty(FREEZE_DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
caster, (int)(getProperty(FREEZE_DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
target.playSound(WizardrySounds.MISC_FREEZE, 1.0F, world.rand.nextFloat() * 0.4F + 0.8F);
}
}else if(target instanceof EntityPlayer){
@@ -113,7 +113,8 @@ public class IceAge extends SpellAreaEffect {
double dist = origin.distanceTo(new Vec3d(origin.x + i, y, origin.z + j));
// Randomised with weighting so that the nearer the block the more likely it is to be snowed.
if(y != -1 && world.rand.nextInt((int)(dist * 2) + 1) < radius && dist < radius){
if(y != -1 && world.rand.nextInt((int)(dist * 2) + 1) < radius && dist < radius
&& BlockUtils.canPlaceBlock(caster, world, pos)){
BlockUtils.freeze(world, pos.down(), true);
}
}
@@ -35,7 +35,7 @@ public class IceStatue extends SpellRay {
if(target instanceof EntityLiving && !world.isRemote){
// Unchecked cast is fine because the block is a static final field
if(((BlockStatue)WizardryBlocks.ice_statue).convertToStatue((EntityLiving)target,
(int)(getProperty(EFFECT_DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
caster, (int)(getProperty(EFFECT_DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
//target.playSound(WizardrySounds.SPELL_FREEZE, 1.0F, world.rand.nextFloat() * 0.4F + 0.8F);
}
@@ -2,6 +2,7 @@ package electroblob.wizardry.spell;
import electroblob.wizardry.item.SpellActions;
import electroblob.wizardry.registry.WizardryItems;
import electroblob.wizardry.util.BlockUtils;
import electroblob.wizardry.util.EntityUtils;
import electroblob.wizardry.util.MagicDamage;
import electroblob.wizardry.util.MagicDamage.DamageType;
@@ -46,13 +47,11 @@ public class Ignite extends SpellRay {
@Override
protected boolean onBlockHit(World world, BlockPos pos, EnumFacing side, Vec3d hit, EntityLivingBase caster, Vec3d origin, int ticksInUse, SpellModifiers modifiers){
if(!EntityUtils.canDamageBlocks(caster, world)) return false;
pos = pos.offset(side);
if(world.isAirBlock(pos)){
if(!world.isRemote){
if(!world.isRemote && BlockUtils.canPlaceBlock(caster, world, pos)){
world.setBlockState(pos, Blocks.FIRE.getDefaultState());
}
@@ -6,8 +6,11 @@ import electroblob.wizardry.item.ISpellCastingItem;
import electroblob.wizardry.item.ItemArtefact;
import electroblob.wizardry.item.SpellActions;
import electroblob.wizardry.registry.WizardryItems;
import electroblob.wizardry.util.*;
import electroblob.wizardry.util.BlockUtils;
import electroblob.wizardry.util.EntityUtils;
import electroblob.wizardry.util.ParticleBuilder;
import electroblob.wizardry.util.ParticleBuilder.Type;
import electroblob.wizardry.util.SpellModifiers;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
@@ -20,8 +23,6 @@ import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import java.lang.reflect.InvocationTargetException;
@@ -62,13 +63,8 @@ public class Mine extends SpellRay {
if(!world.isRemote){
if(BlockUtils.isBlockUnbreakable(world, pos)) return false;
// The mine spell ignores the block damage setting for players, since that's the entire point of the spell
// Instead, it triggers block break events at the appropriate points, which protection mods should be able to
// pick up and allow/disallow accordingly
// For the time being, dispensers respect the mobGriefing gamerule
if(!(caster instanceof EntityPlayer) && !EntityUtils.canDamageBlocks(caster, world)) return false;
// Can't mine arcane-locked blocks
if(world.getTileEntity(pos) != null && world.getTileEntity(pos).getTileData().hasUniqueId(ArcaneLock.NBT_KEY)) return false;
// Reworked to respect the rules, but since we might break multiple blocks this is left as an optimisation
if(!EntityUtils.canDamageBlocks(caster, world)) return false;
IBlockState state = world.getBlockState(pos);
// The maximum harvest level as determined by the potency multiplier. The + 0.5f is so that
@@ -105,13 +101,9 @@ public class Mine extends SpellRay {
boolean silkTouch = state1.getBlock().canSilkHarvest(world, pos1, state1, (EntityPlayer)caster)
&& ItemArtefact.isArtefactActive((EntityPlayer)caster, WizardryItems.charm_silk_touch);
// Some protection mods seem to use this event instead so let's trigger it to check
if(ForgeEventFactory.getBreakSpeed((EntityPlayer)caster, state1, 1, pos1) <= 0) continue;
int xp = BlockUtils.checkBlockBreakXP(caster, world, pos);
int xp = ForgeHooks.onBlockBreakEvent(world,
((EntityPlayerMP)caster).interactionManager.getGameType(), (EntityPlayerMP)caster, pos1);
if(xp == -1) continue; // Event was cancelled
if(xp < 0) continue; // Not allowed to break the block
if(silkTouch){
flag = world.destroyBlock(pos1, false);
@@ -124,7 +116,7 @@ public class Mine extends SpellRay {
if(flag) state1.getBlock().dropXpOnBlockBreak(world, pos1, xp);
}
}else{
}else if(BlockUtils.canBreakBlock(caster, world, pos)){
// NPCs can dig the block under the target's feet
flag = world.destroyBlock(pos1, true) || flag;
}
@@ -32,7 +32,7 @@ public class Petrify extends SpellRay {
if(target instanceof EntityLiving && !world.isRemote){
// Unchecked cast is fine because the block is a static final field
if(((BlockStatue)WizardryBlocks.petrified_stone).convertToStatue((EntityLiving)target,
(int)(getProperty(MINIMUM_EFFECT_DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
caster, (int)(getProperty(MINIMUM_EFFECT_DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
}
}
@@ -49,7 +49,7 @@ public class WallOfFrost extends SpellRay {
if(target instanceof EntityLiving && !world.isRemote){
// Unchecked cast is fine because the block is a static final field
if(((BlockStatue)WizardryBlocks.ice_statue).convertToStatue((EntityLiving)target,
(int)(getProperty(DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
caster, (int)(getProperty(DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade)))){
target.playSound(WizardrySounds.MISC_FREEZE, 1.0F, world.rand.nextFloat() * 0.4F + 0.8F);
}
@@ -77,7 +77,7 @@ public class WallOfFrost extends SpellRay {
int duration = (int)(getProperty(DURATION).floatValue() * modifiers.get(WizardryItems.duration_upgrade));
if(BlockUtils.canBlockBeReplaced(world, pos)){
if(BlockUtils.canBlockBeReplaced(world, pos) && BlockUtils.canPlaceBlock(caster, world, pos)){
world.setBlockState(pos, WizardryBlocks.dry_frosted_ice.getDefaultState());
world.scheduleUpdate(pos.toImmutable(), WizardryBlocks.dry_frosted_ice, duration);
}
@@ -86,7 +86,7 @@ public class WallOfFrost extends SpellRay {
if(side == EnumFacing.UP){
pos = pos.offset(side);
if(BlockUtils.canBlockBeReplaced(world, pos)){
if(BlockUtils.canBlockBeReplaced(world, pos) && BlockUtils.canPlaceBlock(caster, world, pos)){
world.setBlockState(pos, WizardryBlocks.dry_frosted_ice.getDefaultState());
world.scheduleUpdate(pos.toImmutable(), WizardryBlocks.dry_frosted_ice, duration);
}