mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-23 21:38:00 +01:00
Use a single implementation for converting blocks
This commit is contained in:
parent
8d0552995b
commit
c21e492482
6 changed files with 78 additions and 59 deletions
|
@ -17,7 +17,6 @@ import com.minelittlepony.unicopia.util.shape.Sphere;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.block.Blocks;
|
import net.minecraft.block.Blocks;
|
||||||
import net.minecraft.block.Material;
|
|
||||||
import net.minecraft.block.RedstoneWireBlock;
|
import net.minecraft.block.RedstoneWireBlock;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.ItemEntity;
|
import net.minecraft.entity.ItemEntity;
|
||||||
|
@ -76,10 +75,9 @@ public class FireSpell extends AbstractSpell implements Thrown, Attached {
|
||||||
|
|
||||||
protected boolean applyBlocks(World world, BlockPos pos) {
|
protected boolean applyBlocks(World world, BlockPos pos) {
|
||||||
BlockState state = world.getBlockState(pos);
|
BlockState state = world.getBlockState(pos);
|
||||||
Block id = state.getBlock();
|
|
||||||
|
|
||||||
if (id != Blocks.AIR) {
|
if (!state.isAir()) {
|
||||||
if (id == Blocks.NETHERRACK) {
|
if (state.isOf(Blocks.NETHERRACK)) {
|
||||||
if (world.isAir(pos.up())) {
|
if (world.isAir(pos.up())) {
|
||||||
|
|
||||||
if (world.random.nextInt(300) == 0) {
|
if (world.random.nextInt(300) == 0) {
|
||||||
|
@ -88,7 +86,7 @@ public class FireSpell extends AbstractSpell implements Thrown, Attached {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (id == Blocks.REDSTONE_WIRE) {
|
} else if (state.isOf(Blocks.REDSTONE_WIRE)) {
|
||||||
int power = world.random.nextInt(5) == 3 ? 15 : 3;
|
int power = world.random.nextInt(5) == 3 ? 15 : 3;
|
||||||
|
|
||||||
sendPower(world, pos, power, 3, 0);
|
sendPower(world, pos, power, 3, 0);
|
||||||
|
@ -102,21 +100,15 @@ public class FireSpell extends AbstractSpell implements Thrown, Attached {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (state.isIn(BlockTags.LEAVES)) {
|
} else if (state.isIn(BlockTags.LEAVES)) {
|
||||||
if (world.getBlockState(pos.up()).getMaterial() == Material.AIR) {
|
if (world.isAir(pos.up())) {
|
||||||
world.setBlockState(pos.up(), Blocks.FIRE.getDefaultState());
|
world.setBlockState(pos.up(), Blocks.FIRE.getDefaultState());
|
||||||
|
|
||||||
playEffect(world, pos);
|
playEffect(world, pos);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (StateMaps.FIRE_AFFECTED.convert(world, pos)) {
|
||||||
BlockState newState = StateMaps.FIRE_AFFECTED.getConverted(world, state);
|
playEffect(world, pos);
|
||||||
|
return true;
|
||||||
if (!state.equals(newState)) {
|
|
||||||
world.setBlockState(pos, newState, 3);
|
|
||||||
|
|
||||||
playEffect(world, pos);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.minelittlepony.unicopia.ability.magic.Attached;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Caster;
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Thrown;
|
import com.minelittlepony.unicopia.ability.magic.Thrown;
|
||||||
import com.minelittlepony.unicopia.block.state.StateMaps;
|
import com.minelittlepony.unicopia.block.state.StateMaps;
|
||||||
|
import com.minelittlepony.unicopia.particle.ParticleUtils;
|
||||||
import com.minelittlepony.unicopia.util.MagicalDamageSource;
|
import com.minelittlepony.unicopia.util.MagicalDamageSource;
|
||||||
import com.minelittlepony.unicopia.util.PosHelper;
|
import com.minelittlepony.unicopia.util.PosHelper;
|
||||||
import com.minelittlepony.unicopia.util.VecHelper;
|
import com.minelittlepony.unicopia.util.VecHelper;
|
||||||
|
@ -43,7 +44,14 @@ public class IceSpell extends AbstractSpell implements Thrown, Attached {
|
||||||
LivingEntity owner = source.getMaster();
|
LivingEntity owner = source.getMaster();
|
||||||
|
|
||||||
PosHelper.getAllInRegionMutable(source.getOrigin(), effect_range)
|
PosHelper.getAllInRegionMutable(source.getOrigin(), effect_range)
|
||||||
.forEach(i -> applyBlockSingle(owner, source.getWorld(), i));
|
.forEach(i -> {
|
||||||
|
if (applyBlockSingle(owner, source.getWorld(), i)) {
|
||||||
|
ParticleUtils.spawnParticle(ParticleTypes.SPLASH, source.getWorld(), new Vec3d(
|
||||||
|
i.getX() + source.getWorld().random.nextFloat(),
|
||||||
|
i.getY() + 1,
|
||||||
|
i.getZ() + source.getWorld().random.nextFloat()), Vec3d.ZERO);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return applyEntities(source.getMaster(), source.getWorld(), source.getOriginVector());
|
return applyEntities(source.getMaster(), source.getWorld(), source.getOriginVector());
|
||||||
}
|
}
|
||||||
|
@ -65,25 +73,28 @@ public class IceSpell extends AbstractSpell implements Thrown, Attached {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applyBlockSingle(Entity owner, World world, BlockPos pos) {
|
private boolean applyBlockSingle(Entity owner, World world, BlockPos pos) {
|
||||||
BlockState state = world.getBlockState(pos);
|
BlockState state = world.getBlockState(pos);
|
||||||
Block id = state.getBlock();
|
|
||||||
|
|
||||||
BlockState converted = StateMaps.ICE_AFFECTED.getConverted(world, state);
|
if (StateMaps.ICE_AFFECTED.convert(world, pos)) {
|
||||||
|
return true;
|
||||||
if (!state.equals(converted)) {
|
|
||||||
world.setBlockState(pos, converted, 3);
|
|
||||||
} else if (world.isTopSolid(pos, owner)
|
|
||||||
|| (id == Blocks.SNOW)
|
|
||||||
|| state.isIn(BlockTags.LEAVES)) {
|
|
||||||
incrementIce(world, pos.up());
|
|
||||||
} else if (state.getMaterial() == Material.ICE && world.random.nextInt(10) == 0) {
|
|
||||||
if (isSurroundedByIce(world, pos)) {
|
|
||||||
world.setBlockState(pos, Blocks.PACKED_ICE.getDefaultState());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
world.addParticle(ParticleTypes.SPLASH, pos.getX() + world.random.nextFloat(), pos.getY() + 1, pos.getZ() + world.random.nextFloat(), 0, 0, 0);
|
if (world.isTopSolid(pos, owner)
|
||||||
|
|| state.isOf(Blocks.SNOW)
|
||||||
|
|| state.isIn(BlockTags.LEAVES)) {
|
||||||
|
incrementIce(world, pos.up());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.getMaterial() == Material.ICE
|
||||||
|
&& world.random.nextInt(10) == 0
|
||||||
|
&& isSurroundedByIce(world, pos)) {
|
||||||
|
world.setBlockState(pos, Blocks.PACKED_ICE.getDefaultState());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSurroundedByIce(World w, BlockPos pos) {
|
private static boolean isSurroundedByIce(World w, BlockPos pos) {
|
||||||
|
|
|
@ -7,9 +7,6 @@ import com.minelittlepony.unicopia.util.MagicalDamageSource;
|
||||||
import com.minelittlepony.unicopia.util.shape.Shape;
|
import com.minelittlepony.unicopia.util.shape.Shape;
|
||||||
import com.minelittlepony.unicopia.util.shape.Sphere;
|
import com.minelittlepony.unicopia.util.shape.Sphere;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.block.DoorBlock;
|
|
||||||
import net.minecraft.block.enums.DoubleBlockHalf;
|
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.LivingEntity;
|
import net.minecraft.entity.LivingEntity;
|
||||||
import net.minecraft.entity.damage.DamageSource;
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
@ -42,20 +39,7 @@ public class InfernoSpell extends FireSpell {
|
||||||
for (int i = 0; i < radius; i++) {
|
for (int i = 0; i < radius; i++) {
|
||||||
BlockPos pos = new BlockPos(shape.computePoint(w.random).add(origin));
|
BlockPos pos = new BlockPos(shape.computePoint(w.random).add(origin));
|
||||||
|
|
||||||
BlockState state = w.getBlockState(pos);
|
if (converter.convert(w, pos)) {
|
||||||
BlockState newState = converter.getConverted(w, state);
|
|
||||||
|
|
||||||
if (!state.equals(newState)) {
|
|
||||||
|
|
||||||
if (newState.getBlock() instanceof DoorBlock) {
|
|
||||||
boolean lower = newState.get(DoorBlock.HALF) == DoubleBlockHalf.LOWER;
|
|
||||||
BlockPos other = lower ? pos.up() : pos.down();
|
|
||||||
|
|
||||||
w.setBlockState(other, newState.with(DoorBlock.HALF, lower ? DoubleBlockHalf.UPPER : DoubleBlockHalf.LOWER), 16 | 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
w.setBlockState(pos, newState, 16 | 2);
|
|
||||||
|
|
||||||
playEffect(w, pos);
|
playEffect(w, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import com.minelittlepony.unicopia.projectile.MagicProjectileEntity;
|
||||||
import com.minelittlepony.unicopia.util.PosHelper;
|
import com.minelittlepony.unicopia.util.PosHelper;
|
||||||
import com.minelittlepony.unicopia.util.shape.Sphere;
|
import com.minelittlepony.unicopia.util.shape.Sphere;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.particle.ParticleTypes;
|
import net.minecraft.particle.ParticleTypes;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.Vec3d;
|
import net.minecraft.util.math.Vec3d;
|
||||||
|
@ -25,12 +24,7 @@ public class ScorchSpell extends FireSpell {
|
||||||
|
|
||||||
BlockPos pos = PosHelper.findSolidGroundAt(source.getWorld(), source.getOrigin(), source.getPhysics().getGravitySignum());
|
BlockPos pos = PosHelper.findSolidGroundAt(source.getWorld(), source.getOrigin(), source.getPhysics().getGravitySignum());
|
||||||
|
|
||||||
BlockState state = source.getWorld().getBlockState(pos);
|
if (StateMaps.FIRE_AFFECTED.convert(source.getWorld(), pos)) {
|
||||||
|
|
||||||
BlockState newState = StateMaps.FIRE_AFFECTED.getConverted(source.getWorld(), state);
|
|
||||||
|
|
||||||
if (!state.equals(newState)) {
|
|
||||||
source.getWorld().setBlockState(pos, newState, 3);
|
|
||||||
source.spawnParticles(new Sphere(false, 1), 5, p -> {
|
source.spawnParticles(new Sphere(false, 1), 5, p -> {
|
||||||
source.addParticle(ParticleTypes.SMOKE, PosHelper.offset(p, pos), Vec3d.ZERO);
|
source.addParticle(ParticleTypes.SMOKE, PosHelper.offset(p, pos), Vec3d.ZERO);
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,6 +4,9 @@ import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.enums.DoubleBlockHalf;
|
||||||
|
import net.minecraft.state.property.Properties;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public interface BlockStateConverter {
|
public interface BlockStateConverter {
|
||||||
|
@ -19,10 +22,47 @@ public interface BlockStateConverter {
|
||||||
/**
|
/**
|
||||||
* Attempts to convert the given state based on the known mappings in this collection.
|
* Attempts to convert the given state based on the known mappings in this collection.
|
||||||
*
|
*
|
||||||
|
* @param world The world
|
||||||
* @param state State to convert
|
* @param state State to convert
|
||||||
*
|
*
|
||||||
* @return The converted state if there is one, otherwise null
|
* @return The converted state if there is one, otherwise the original state is returned
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
BlockState getConverted(World world, @Nonnull BlockState state);
|
BlockState getConverted(World world, @Nonnull BlockState state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to convert a block state at a position.
|
||||||
|
* Returns true if the block was changed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
default boolean convert(World world, BlockPos pos) {
|
||||||
|
BlockState state = world.getBlockState(pos);
|
||||||
|
|
||||||
|
if (!canConvert(state)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockState newState = getConverted(world, state);
|
||||||
|
|
||||||
|
if (state.equals(newState)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newState.contains(Properties.DOUBLE_BLOCK_HALF)) {
|
||||||
|
world.setBlockState(pos, newState, 16 | 2);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean lower = newState.get(Properties.DOUBLE_BLOCK_HALF) == DoubleBlockHalf.LOWER;
|
||||||
|
BlockPos other = lower ? pos.up() : pos.down();
|
||||||
|
|
||||||
|
if (world.getBlockState(other).isOf(state.getBlock())) {
|
||||||
|
world.setBlockState(other, newState.with(Properties.DOUBLE_BLOCK_HALF, lower ? DoubleBlockHalf.UPPER : DoubleBlockHalf.LOWER), 16 | 2);
|
||||||
|
world.setBlockState(pos, newState, 16 | 2);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,7 @@ public class SilverfishBehaviour extends EntityBehaviour<SilverfishEntity> {
|
||||||
BlockPos pos = entity.getBlockPos().down();
|
BlockPos pos = entity.getBlockPos().down();
|
||||||
BlockState state = entity.world.getBlockState(pos);
|
BlockState state = entity.world.getBlockState(pos);
|
||||||
|
|
||||||
if (StateMaps.SILVERFISH_AFFECTED.canConvert(state)) {
|
if (StateMaps.SILVERFISH_AFFECTED.convert(entity.world, pos)) {
|
||||||
|
|
||||||
entity.world.setBlockState(pos, StateMaps.SILVERFISH_AFFECTED.getConverted(entity.world, state));
|
|
||||||
WorldEvent.play(WorldEvent.DESTROY_BLOCK, entity.world, pos, state);
|
WorldEvent.play(WorldEvent.DESTROY_BLOCK, entity.world, pos, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue