Refactored the Explosive Charges to have a proper base class.

This commit is contained in:
WayofTime 2021-01-23 14:52:46 -05:00
parent cac20388f8
commit 0a0742d8eb
6 changed files with 118 additions and 102 deletions

View file

@ -18,6 +18,7 @@ import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld; import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader; import net.minecraft.world.IWorldReader;
import net.minecraft.world.World; import net.minecraft.world.World;
import wayoftime.bloodmagic.tile.TileExplosiveCharge;
import wayoftime.bloodmagic.tile.TileShapedExplosive; import wayoftime.bloodmagic.tile.TileShapedExplosive;
public class BlockShapedExplosive extends Block public class BlockShapedExplosive extends Block
@ -110,7 +111,7 @@ public class BlockShapedExplosive extends Block
@Override @Override
public void onBlockHarvested(World world, BlockPos blockPos, BlockState blockState, PlayerEntity player) public void onBlockHarvested(World world, BlockPos blockPos, BlockState blockState, PlayerEntity player)
{ {
TileShapedExplosive tile = (TileShapedExplosive) world.getTileEntity(blockPos); TileExplosiveCharge tile = (TileExplosiveCharge) world.getTileEntity(blockPos);
if (tile != null && !world.isRemote) if (tile != null && !world.isRemote)
tile.dropSelf(); tile.dropSelf();

View file

@ -14,7 +14,7 @@ import net.minecraft.util.SoundEvents;
import net.minecraft.world.World; import net.minecraft.world.World;
import wayoftime.bloodmagic.anointment.AnointmentHolder; import wayoftime.bloodmagic.anointment.AnointmentHolder;
import wayoftime.bloodmagic.entity.projectile.EntityShapedCharge; import wayoftime.bloodmagic.entity.projectile.EntityShapedCharge;
import wayoftime.bloodmagic.tile.TileShapedExplosive; import wayoftime.bloodmagic.tile.TileExplosiveCharge;
public class ItemBlockShapedCharge extends BlockItem public class ItemBlockShapedCharge extends BlockItem
{ {
@ -60,9 +60,9 @@ public class ItemBlockShapedCharge extends BlockItem
if (holder != null) if (holder != null)
{ {
TileEntity tile = context.getWorld().getTileEntity(context.getPos()); TileEntity tile = context.getWorld().getTileEntity(context.getPos());
if (tile instanceof TileShapedExplosive) if (tile instanceof TileExplosiveCharge)
{ {
((TileShapedExplosive) tile).setAnointmentHolder(holder); ((TileExplosiveCharge) tile).setAnointmentHolder(holder);
} }
} }

View file

@ -24,7 +24,7 @@ import wayoftime.bloodmagic.anointment.AnointmentHolder;
import wayoftime.bloodmagic.common.block.BlockShapedExplosive; import wayoftime.bloodmagic.common.block.BlockShapedExplosive;
import wayoftime.bloodmagic.common.block.BloodMagicBlocks; import wayoftime.bloodmagic.common.block.BloodMagicBlocks;
import wayoftime.bloodmagic.common.registries.BloodMagicEntityTypes; import wayoftime.bloodmagic.common.registries.BloodMagicEntityTypes;
import wayoftime.bloodmagic.tile.TileShapedExplosive; import wayoftime.bloodmagic.tile.TileExplosiveCharge;
public class EntityShapedCharge extends ThrowableEntity public class EntityShapedCharge extends ThrowableEntity
{ {
@ -75,9 +75,9 @@ public class EntityShapedCharge extends ThrowableEntity
{ {
this.getEntityWorld().setBlockState(blockpos, fallTile.with(BlockShapedExplosive.ATTACHED, faceHit)); this.getEntityWorld().setBlockState(blockpos, fallTile.with(BlockShapedExplosive.ATTACHED, faceHit));
TileEntity tile = this.getEntityWorld().getTileEntity(blockpos); TileEntity tile = this.getEntityWorld().getTileEntity(blockpos);
if (tile instanceof TileShapedExplosive) if (tile instanceof TileExplosiveCharge)
{ {
((TileShapedExplosive) tile).setAnointmentHolder(holder); ((TileExplosiveCharge) tile).setAnointmentHolder(holder);
} }
this.setDead(); this.setDead();
} else } else

View file

@ -11,7 +11,6 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
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.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootContext; import net.minecraft.loot.LootContext;
@ -30,7 +29,7 @@ import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.registries.ObjectHolder; import net.minecraftforge.registries.ObjectHolder;
import wayoftime.bloodmagic.common.block.BlockShapedExplosive; import wayoftime.bloodmagic.common.block.BlockShapedExplosive;
public class TileDeforesterCharge extends TileShapedExplosive public class TileDeforesterCharge extends TileExplosiveCharge
{ {
@ObjectHolder("bloodmagic:deforester_charge") @ObjectHolder("bloodmagic:deforester_charge")
public static TileEntityType<TileDeforesterCharge> TYPE; public static TileEntityType<TileDeforesterCharge> TYPE;
@ -50,16 +49,16 @@ public class TileDeforesterCharge extends TileShapedExplosive
public int maxLogs = 128; public int maxLogs = 128;
public TileDeforesterCharge(TileEntityType<?> type, int explosionRadius, int explosionDepth) public TileDeforesterCharge(TileEntityType<?> type, int maxLogs)
{ {
super(type, explosionRadius, explosionDepth); super(type);
// this.explosionRadius = explosionRadius;
// this.explosionDepth = explosionDepth; this.maxLogs = maxLogs;
} }
public TileDeforesterCharge() public TileDeforesterCharge()
{ {
this(TYPE, 1, 3); this(TYPE, 128);
} }
@Override @Override
@ -174,7 +173,7 @@ public class TileDeforesterCharge extends TileShapedExplosive
ItemStack toolStack = this.getHarvestingTool(); ItemStack toolStack = this.getHarvestingTool();
world.playSound((PlayerEntity) null, this.getPos().getX() + 0.5, this.getPos().getY() + 0.5, this.getPos().getZ() + 0.5, SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.BLOCKS, 4.0F, (1.0F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.2F) * 0.7F); world.playSound((PlayerEntity) null, this.getPos().getX() + 0.5, this.getPos().getY() + 0.5, this.getPos().getZ() + 0.5, SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.BLOCKS, 4.0F, (1.0F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.2F) * 0.7F);
int numParticles = explosionDepth * (explosionRadius + 1); int numParticles = 10;
((ServerWorld) this.world).spawnParticle(ParticleTypes.EXPLOSION, pos.getX() + 0.5 + explosiveDirection.getXOffset(), pos.getY() + 0.5 + explosiveDirection.getYOffset(), pos.getZ() + 0.5 + explosiveDirection.getZOffset(), numParticles, 1.0D, 1.0D, 1.0D, 0); ((ServerWorld) this.world).spawnParticle(ParticleTypes.EXPLOSION, pos.getX() + 0.5 + explosiveDirection.getXOffset(), pos.getY() + 0.5 + explosiveDirection.getYOffset(), pos.getZ() + 0.5 + explosiveDirection.getZOffset(), numParticles, 1.0D, 1.0D, 1.0D, 0);
@ -219,28 +218,6 @@ public class TileDeforesterCharge extends TileShapedExplosive
} }
} }
private static void handleExplosionDrops(ObjectArrayList<Pair<ItemStack, BlockPos>> dropPositionArray, ItemStack stack, BlockPos pos)
{
int i = dropPositionArray.size();
for (int j = 0; j < i; ++j)
{
Pair<ItemStack, BlockPos> pair = dropPositionArray.get(j);
ItemStack itemstack = pair.getFirst();
if (ItemEntity.canMergeStacks(itemstack, stack))
{
ItemStack itemstack1 = ItemEntity.mergeStacks(itemstack, stack, 16);
dropPositionArray.set(j, Pair.of(itemstack1, pair.getSecond()));
if (stack.isEmpty())
{
return;
}
}
}
dropPositionArray.add(Pair.of(stack, pos));
}
@Override @Override
public void deserialize(CompoundNBT tag) public void deserialize(CompoundNBT tag)
{ {

View file

@ -0,0 +1,98 @@
package wayoftime.bloodmagic.tile;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.math.BlockPos;
import wayoftime.bloodmagic.anointment.AnointmentHolder;
import wayoftime.bloodmagic.tile.base.TileTicking;
public class TileExplosiveCharge extends TileTicking
{
public AnointmentHolder anointmentHolder = new AnointmentHolder();
public TileExplosiveCharge(TileEntityType<?> type)
{
super(type);
}
protected static void handleExplosionDrops(ObjectArrayList<Pair<ItemStack, BlockPos>> dropPositionArray, ItemStack stack, BlockPos pos)
{
int i = dropPositionArray.size();
for (int j = 0; j < i; ++j)
{
Pair<ItemStack, BlockPos> pair = dropPositionArray.get(j);
ItemStack itemstack = pair.getFirst();
if (ItemEntity.canMergeStacks(itemstack, stack))
{
ItemStack itemstack1 = ItemEntity.mergeStacks(itemstack, stack, 16);
dropPositionArray.set(j, Pair.of(itemstack1, pair.getSecond()));
if (stack.isEmpty())
{
return;
}
}
}
dropPositionArray.add(Pair.of(stack, pos));
}
public ItemStack getHarvestingTool()
{
ItemStack stack = new ItemStack(Items.DIAMOND_PICKAXE);
if (anointmentHolder != null)
anointmentHolder.toItemStack(stack);
return stack;
}
@Override
public void deserialize(CompoundNBT tag)
{
if (tag.contains("holder"))
{
anointmentHolder = AnointmentHolder.fromNBT(tag.getCompound("holder"));
}
}
@Override
public CompoundNBT serialize(CompoundNBT tag)
{
if (anointmentHolder != null)
{
tag.put("holder", anointmentHolder.serialize());
}
return tag;
}
public void setAnointmentHolder(AnointmentHolder holder)
{
this.anointmentHolder = holder;
}
public void dropSelf()
{
ItemStack stack = new ItemStack(getBlockState().getBlock());
if (anointmentHolder != null && !anointmentHolder.isEmpty())
{
anointmentHolder.toItemStack(stack);
}
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
}
@Override
public void onUpdate()
{
}
}

View file

@ -6,11 +6,8 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
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.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.loot.LootContext; import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters; import net.minecraft.loot.LootParameters;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
@ -24,21 +21,17 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.server.ServerWorld; import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.registries.ObjectHolder; import net.minecraftforge.registries.ObjectHolder;
import wayoftime.bloodmagic.anointment.AnointmentHolder;
import wayoftime.bloodmagic.common.block.BlockShapedExplosive; import wayoftime.bloodmagic.common.block.BlockShapedExplosive;
import wayoftime.bloodmagic.tile.base.TileTicking;
public class TileShapedExplosive extends TileTicking public class TileShapedExplosive extends TileExplosiveCharge
{ {
@ObjectHolder("bloodmagic:shaped_explosive") @ObjectHolder("bloodmagic:shaped_explosive")
public static TileEntityType<TileShapedExplosive> TYPE; public static TileEntityType<TileExplosiveCharge> TYPE;
public double internalCounter = 0; public double internalCounter = 0;
public int explosionRadius; public int explosionRadius;
public int explosionDepth; public int explosionDepth;
public AnointmentHolder anointmentHolder = new AnointmentHolder();
public TileShapedExplosive(TileEntityType<?> type, int explosionRadius, int explosionDepth) public TileShapedExplosive(TileEntityType<?> type, int explosionRadius, int explosionDepth)
{ {
super(type); super(type);
@ -165,72 +158,19 @@ public class TileShapedExplosive extends TileTicking
} }
} }
private static void handleExplosionDrops(ObjectArrayList<Pair<ItemStack, BlockPos>> dropPositionArray, ItemStack stack, BlockPos pos)
{
int i = dropPositionArray.size();
for (int j = 0; j < i; ++j)
{
Pair<ItemStack, BlockPos> pair = dropPositionArray.get(j);
ItemStack itemstack = pair.getFirst();
if (ItemEntity.canMergeStacks(itemstack, stack))
{
ItemStack itemstack1 = ItemEntity.mergeStacks(itemstack, stack, 16);
dropPositionArray.set(j, Pair.of(itemstack1, pair.getSecond()));
if (stack.isEmpty())
{
return;
}
}
}
dropPositionArray.add(Pair.of(stack, pos));
}
public ItemStack getHarvestingTool()
{
ItemStack stack = new ItemStack(Items.DIAMOND_PICKAXE);
if (anointmentHolder != null)
anointmentHolder.toItemStack(stack);
return stack;
}
@Override @Override
public void deserialize(CompoundNBT tag) public void deserialize(CompoundNBT tag)
{ {
super.deserialize(tag);
internalCounter = tag.getDouble("internalCounter"); internalCounter = tag.getDouble("internalCounter");
if (tag.contains("holder"))
{
anointmentHolder = AnointmentHolder.fromNBT(tag.getCompound("holder"));
}
} }
@Override @Override
public CompoundNBT serialize(CompoundNBT tag) public CompoundNBT serialize(CompoundNBT tag)
{ {
super.serialize(tag);
tag.putDouble("internalCounter", internalCounter); tag.putDouble("internalCounter", internalCounter);
if (anointmentHolder != null)
{
tag.put("holder", anointmentHolder.serialize());
}
return tag; return tag;
} }
}
public void setAnointmentHolder(AnointmentHolder holder)
{
this.anointmentHolder = holder;
}
public void dropSelf()
{
ItemStack stack = new ItemStack(getBlockState().getBlock());
if (anointmentHolder != null && !anointmentHolder.isEmpty())
{
anointmentHolder.toItemStack(stack);
}
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}