Created
September 22, 2021 10:06
-
-
Save CATboardBETA/19ab17df8fd37821ef892ffa7c2f5e7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package personal.catboardbeta.morebombs.util; | |
import com.google.common.collect.Sets; | |
import net.minecraft.block.Block; | |
import net.minecraft.block.BlockState; | |
import net.minecraft.block.Blocks; | |
import net.minecraft.entity.Entity; | |
import net.minecraft.entity.item.ItemEntity; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.util.math.AxisAlignedBB; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.util.math.MathHelper; | |
import net.minecraft.world.World; | |
import java.util.*; | |
import static personal.catboardbeta.morebombs.Morebombs.LOGGER; | |
public class ExplosionFactory { | |
public enum ExplosionType { | |
CLEAR, BREAK | |
} | |
private int radius; | |
private ExplosionType type; | |
private int x; | |
private int y; | |
private int z; | |
private final World world; | |
private Entity tileEntity; | |
public ExplosionFactory(World world, Entity tile) { | |
LOGGER.info("Morebombs: Initiated the ExplosionFactory"); | |
this.world = world; | |
this.setEntity(tile); | |
this.x = (int) tileEntity.getX(); | |
this.y = (int) tileEntity.getY(); | |
this.z = (int) tileEntity.getZ(); | |
} | |
public ExplosionFactory explode() { | |
LOGGER.info("Entered ExplosionFactory::explode()"); | |
Set<BlockPos> blocksToBlow = toBlow(); | |
for (BlockPos block : blocksToBlow) { | |
switch (this.type) { | |
case CLEAR: | |
world.setBlockAndUpdate(block, Blocks.AIR.defaultBlockState()); | |
break; | |
case BREAK: | |
world.destroyBlock(block, true, this.getEntity()); | |
BlockState bs = world.getBlockState(block); | |
List<ItemStack> blockDrops = new ArrayList<>(); | |
blockDrops.add(bs.getBlock().asItem().getDefaultInstance()); | |
for (ItemStack stack : blockDrops) { | |
world.addFreshEntity(new ItemEntity(world, block.getX(), block.getY(), block.getZ(), stack)); | |
} | |
break; | |
default: | |
LOGGER.error("Morebombs: Explosion at (" + x +", " + y + ", " + z + ") has invalid ExplosionType." + | |
"Valid types: " + Arrays.toString(ExplosionType.values())); | |
break; | |
} | |
} | |
return this; | |
} | |
private Set<BlockPos> toBlow() { | |
Set<BlockPos> blocksToBlow = Sets.newHashSet(); | |
for (int i = x - radius; i <= x + radius; ++i) { | |
for (int j = y - radius; j <= y + radius; ++j) { | |
for (int k = z - radius; k <= z + radius; ++k) { | |
LOGGER.info(i + " " + j + " " + k); | |
BlockPos pos = this.getEntity().getEntity().blockPosition(); | |
BlockPos block = pos.offset(i, j, k); | |
if (!isDistanceTooMuch(pos, block, radius)) { | |
blocksToBlow.add(block); | |
} | |
} | |
} | |
} | |
LOGGER.info(blocksToBlow); | |
return blocksToBlow; | |
} | |
private boolean isDistanceTooMuch(BlockPos startPos, BlockPos endPos, int allowedDistance) { | |
int startPosX = startPos.getX(); | |
int startPosY = startPos.getY(); | |
int startPosZ = startPos.getZ(); | |
int endPosX = endPos.getX(); | |
int endPosY = endPos.getY(); | |
int endPosZ = endPos.getZ(); | |
return distance3d(startPosX, startPosY, startPosZ, endPosX, endPosY, endPosZ) <= allowedDistance; | |
} | |
private int distance3d(int x1, int y1, int z1, int x2, int y2, int z2) { | |
return MathHelper.fastFloor( | |
Math.sqrt( | |
MathHelper.square(x1 - x2) + | |
MathHelper.square(y1 - y2) + | |
MathHelper.square(z1 - z2) | |
)); | |
} | |
public int round(float x) { | |
if (x < MathHelper.fastFloor(x) + 0.5F) { | |
return MathHelper.fastFloor(x); | |
} | |
return MathHelper.ceil(x); | |
} | |
// 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.areMergable(itemstack, stack)) { | |
// ItemStack itemstack1 = ItemEntity.merge(itemstack, stack, 16); | |
// dropPositionArray.set(j, Pair.of(itemstack1, pair.getSecond())); | |
// if (stack.isEmpty()) { | |
// return; | |
// } | |
// } | |
// } | |
// dropPositionArray.add(Pair.of(stack, pos)); | |
// } | |
// | |
// ***************************************************************************************************************** | |
// Getters/Setters | |
public int getRadius() { | |
return radius; | |
} | |
public ExplosionFactory setRadius(int radius) { | |
this.radius = radius; | |
return this; | |
} | |
public int getX() { | |
return x; | |
} | |
public ExplosionFactory setX(int x) { | |
this.x = x; | |
return this; | |
} | |
public int getY() { | |
return y; | |
} | |
public ExplosionFactory setY(int y) { | |
this.y = y; | |
return this; | |
} | |
public int getZ() { | |
return z; | |
} | |
public ExplosionFactory setZ(int z) { | |
this.z = z; | |
return this; | |
} | |
public ExplosionFactory setCoords(BlockPos coords) { | |
this.x = coords.getX(); | |
this.y = coords.getY(); | |
this.z = coords.getZ(); | |
return this; | |
} | |
public BlockPos getCoords() { | |
return new BlockPos(x, y, z); | |
} | |
public ExplosionType getExplosionType() { | |
return type; | |
} | |
public ExplosionFactory setExplosionType(ExplosionType type) { | |
this.type = type; | |
return this; | |
} | |
public Entity getEntity() { | |
return tileEntity; | |
} | |
public ExplosionFactory setEntity(Entity tileEntity) { | |
this.tileEntity = tileEntity; | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package personal.catboardbeta.morebombs.common.blocks; | |
import net.minecraft.block.*; | |
import net.minecraft.entity.Entity; | |
import net.minecraft.entity.LivingEntity; | |
import net.minecraft.entity.item.TNTEntity; | |
import net.minecraft.entity.player.PlayerEntity; | |
import net.minecraft.entity.projectile.ProjectileEntity; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.item.Items; | |
import net.minecraft.state.BooleanProperty; | |
import net.minecraft.state.StateContainer; | |
import net.minecraft.state.properties.BlockStateProperties; | |
import net.minecraft.util.ActionResultType; | |
import net.minecraft.util.Hand; | |
import net.minecraft.util.SoundCategory; | |
import net.minecraft.util.SoundEvents; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.util.math.BlockRayTraceResult; | |
import net.minecraft.world.Explosion; | |
import net.minecraft.world.World; | |
import personal.catboardbeta.morebombs.common.entities.TNT10xEntity; | |
import javax.annotation.Nullable; | |
public class TNT10x extends Block { | |
public static final BooleanProperty UNSTABLE = BlockStateProperties.UNSTABLE; | |
public TNT10x(AbstractBlock.Properties p_i48309_1_) { | |
super(p_i48309_1_); | |
this.registerDefaultState(this.defaultBlockState().setValue(UNSTABLE, Boolean.valueOf(false))); | |
} | |
public void catchFire(BlockState state, World world, BlockPos pos, @Nullable net.minecraft.util.Direction face, @Nullable LivingEntity igniter) { | |
explode(world, pos, igniter); | |
} | |
public void onPlace(BlockState p_220082_1_, World p_220082_2_, BlockPos p_220082_3_, BlockState p_220082_4_, boolean p_220082_5_) { | |
if (!p_220082_4_.is(p_220082_1_.getBlock())) { | |
if (p_220082_2_.hasNeighborSignal(p_220082_3_)) { | |
catchFire(p_220082_1_, p_220082_2_, p_220082_3_, null, null); | |
p_220082_2_.removeBlock(p_220082_3_, false); | |
} | |
} | |
} | |
public void neighborChanged(BlockState p_220069_1_, World p_220069_2_, BlockPos p_220069_3_, Block p_220069_4_, BlockPos p_220069_5_, boolean p_220069_6_) { | |
if (p_220069_2_.hasNeighborSignal(p_220069_3_)) { | |
catchFire(p_220069_1_, p_220069_2_, p_220069_3_, null, null); | |
p_220069_2_.removeBlock(p_220069_3_, false); | |
} | |
} | |
public void playerWillDestroy(World p_176208_1_, BlockPos p_176208_2_, BlockState p_176208_3_, PlayerEntity p_176208_4_) { | |
if (!p_176208_1_.isClientSide() && !p_176208_4_.isCreative() && p_176208_3_.getValue(UNSTABLE)) { | |
catchFire(p_176208_3_, p_176208_1_, p_176208_2_, null, null); | |
} | |
super.playerWillDestroy(p_176208_1_, p_176208_2_, p_176208_3_, p_176208_4_); | |
} | |
public void wasExploded(World p_180652_1_, BlockPos p_180652_2_, Explosion p_180652_3_) { | |
if (!p_180652_1_.isClientSide) { | |
TNTEntity tntentity = new TNTEntity(p_180652_1_, (double)p_180652_2_.getX() + 0.5D, (double)p_180652_2_.getY(), (double)p_180652_2_.getZ() + 0.5D, p_180652_3_.getSourceMob()); | |
tntentity.setFuse((short)(p_180652_1_.random.nextInt(tntentity.getLife() / 4) + tntentity.getLife() / 8)); | |
p_180652_1_.addFreshEntity(tntentity); | |
} | |
} | |
@Deprecated //Forge: Prefer using IForgeBlock#catchFire | |
public static void explode(World p_196534_0_, BlockPos p_196534_1_) { | |
explode(p_196534_0_, p_196534_1_, (LivingEntity)null); | |
} | |
@Deprecated //Forge: Prefer using IForgeBlock#catchFire | |
private static void explode(World p_196535_0_, BlockPos p_196535_1_, @Nullable LivingEntity p_196535_2_) { | |
if (!p_196535_0_.isClientSide) { | |
TNT10xEntity tntentity = new TNT10xEntity(p_196535_0_, (double)p_196535_1_.getX() + 0.5D, (double)p_196535_1_.getY(), (double)p_196535_1_.getZ() + 0.5D, p_196535_2_); | |
p_196535_0_.addFreshEntity(tntentity); | |
p_196535_0_.playSound((PlayerEntity)null, tntentity.getX(), tntentity.getY(), tntentity.getZ(), SoundEvents.TNT_PRIMED, SoundCategory.BLOCKS, 1.0F, 1.0F); | |
} | |
} | |
public ActionResultType use(BlockState p_225533_1_, World p_225533_2_, BlockPos p_225533_3_, PlayerEntity p_225533_4_, Hand p_225533_5_, BlockRayTraceResult p_225533_6_) { | |
ItemStack itemstack = p_225533_4_.getItemInHand(p_225533_5_); | |
Item item = itemstack.getItem(); | |
if (item != Items.FLINT_AND_STEEL && item != Items.FIRE_CHARGE) { | |
return super.use(p_225533_1_, p_225533_2_, p_225533_3_, p_225533_4_, p_225533_5_, p_225533_6_); | |
} else { | |
catchFire(p_225533_1_, p_225533_2_, p_225533_3_, p_225533_6_.getDirection(), p_225533_4_); | |
p_225533_2_.setBlock(p_225533_3_, Blocks.AIR.defaultBlockState(), 11); | |
if (!p_225533_4_.isCreative()) { | |
if (item == Items.FLINT_AND_STEEL) { | |
itemstack.hurtAndBreak(1, p_225533_4_, (p_220287_1_) -> { | |
p_220287_1_.broadcastBreakEvent(p_225533_5_); | |
}); | |
} else { | |
itemstack.shrink(1); | |
} | |
} | |
return ActionResultType.sidedSuccess(p_225533_2_.isClientSide); | |
} | |
} | |
public void onProjectileHit(World p_220066_1_, BlockState p_220066_2_, BlockRayTraceResult p_220066_3_, ProjectileEntity p_220066_4_) { | |
if (!p_220066_1_.isClientSide) { | |
Entity entity = p_220066_4_.getOwner(); | |
if (p_220066_4_.isOnFire()) { | |
BlockPos blockpos = p_220066_3_.getBlockPos(); | |
catchFire(p_220066_2_, p_220066_1_, blockpos, null, entity instanceof LivingEntity ? (LivingEntity)entity : null); | |
p_220066_1_.removeBlock(blockpos, false); | |
} | |
} | |
} | |
public boolean dropFromExplosion(Explosion p_149659_1_) { | |
return false; | |
} | |
protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> p_206840_1_) { | |
p_206840_1_.add(UNSTABLE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package personal.catboardbeta.morebombs.common.entities; | |
import net.minecraft.entity.EntityType; | |
import net.minecraft.entity.LivingEntity; | |
import net.minecraft.entity.item.TNTEntity; | |
import net.minecraft.world.World; | |
import personal.catboardbeta.morebombs.util.ExplosionFactory; | |
import javax.annotation.Nullable; | |
import personal.catboardbeta.morebombs.util.ExplosionFactory.ExplosionType; | |
public class TNT10xEntity extends TNTEntity { | |
private LivingEntity owner; | |
public TNT10xEntity(EntityType<? extends TNTEntity> type, World world) { | |
super(type, world); | |
} | |
public TNT10xEntity(World world, double x, double y, double z, @Nullable LivingEntity lighter) { | |
this(EntityType.TNT, world); | |
this.setPos(x, y, z); | |
double d0 = world.random.nextDouble() * (double)((float)Math.PI * 2F); | |
this.setDeltaMovement(-Math.sin(d0) * 0.02D, (double)0.2F, -Math.cos(d0) * 0.02D); | |
this.setFuse(80); | |
this.xo = x; | |
this.yo = y; | |
this.zo = z; | |
this.owner = lighter; | |
} | |
@Override | |
public void tick() { | |
LOGGER.info("Entered TNT10xEntity::tick()"); | |
ExplosionFactory es = new ExplosionFactory(this.level, this); | |
es.setCoords(this.getOnPos()).setExplosionType(ExplosionType.CLEAR).setRadius(4); | |
es.explode(); | |
this.remove(); | |
} | |
@Override | |
protected void explode() { | |
// empty | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment