Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MBatt1 committed May 5, 2024
1 parent b46cc87 commit c2104db
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package net.id.paradiselost.mixin.entity;

import net.id.paradiselost.util.BloomedCalciteUtil;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.projectile.thrown.PotionEntity;
import net.minecraft.entity.projectile.thrown.ThrownItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionUtil;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
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 java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Mixin(PotionEntity.class)
public class PotionEntityMixin extends ThrownItemEntity {

public PotionEntityMixin(EntityType<? extends ThrownItemEntity> entityType, World world) {
super(entityType, world);
}

@Inject(method = "onBlockHit", at = @At("TAIL"), cancellable = true)
protected void onBlockHit(BlockHitResult blockHitResult, CallbackInfo ci) {
if (!this.world.isClient) {
ItemStack itemStack = this.getStack();
List<StatusEffectInstance> list = PotionUtil.getPotionEffects(itemStack);
boolean healingPotion = list.stream().anyMatch((e) -> e.getEffectType() == StatusEffects.INSTANT_HEALTH);
Direction direction = blockHitResult.getSide();
BlockPos blockPos = blockHitResult.getBlockPos();
BlockPos landBlock = blockPos.offset(direction);
if (healingPotion) {
List<BlockPos> affected = new LinkedList<>();
if (world.getBlockState(landBlock.up()).isOf(Blocks.CALCITE)) {
affected.add(landBlock.up());
}
if (world.getBlockState(landBlock.down()).isOf(Blocks.CALCITE)) {
affected.add(landBlock.down());
}
addIfValid(landBlock, affected);
addIfValid(landBlock.up(), affected);
addIfValid(landBlock.down(), affected);
for (Direction dir : Direction.Type.HORIZONTAL) {
addIfValid(landBlock.up().offset(dir), affected);
addIfValid(landBlock.down().offset(dir), affected);
}
for (BlockPos pos : new BlockPos[] {landBlock, landBlock.north(), landBlock.south()}) {
addIfValid(pos, affected);
addIfValid(pos.east(), affected);
addIfValid(pos.west(), affected);
}
if (!affected.isEmpty()) {
Collections.shuffle(affected);
BloomedCalciteUtil.applyHealing(this.getOwner(), world, affected.get(0), this.world.random, itemStack);
if (affected.size() > 1 && this.world.random.nextBoolean()) BloomedCalciteUtil.applyHealing(this.getOwner(), world, affected.get(1), this.world.random, itemStack);
}
}

}
}

private void addIfValid(BlockPos pos, List<BlockPos> list) {
if (world.getBlockState(pos).isOf(Blocks.CALCITE)) {
list.add(pos);
}
}

@Override
public Item getDefaultItem() {
return null;
}
}
18 changes: 5 additions & 13 deletions src/main/java/net/id/paradiselost/mixin/item/PotionItemMixin.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.id.paradiselost.mixin.item;

import net.id.paradiselost.blocks.ParadiseLostBlocks;
import net.id.paradiselost.util.BloomedCalciteUtil;
import net.kyrptonaught.customportalapi.mixin.portalLighters.PotionEntityMixin;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.thrown.PotionEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsage;
import net.minecraft.item.ItemUsageContext;
Expand Down Expand Up @@ -36,21 +39,10 @@ public void useOnBlock(ItemUsageContext context, CallbackInfoReturnable<ActionRe
ItemStack itemStack = context.getStack();
BlockState blockState = world.getBlockState(blockPos);
Random random = world.random;
if (blockState.isOf(Blocks.CALCITE) && (PotionUtil.getPotion(itemStack) == Potions.HEALING || PotionUtil.getPotion(itemStack) == Potions.STRONG_HEALING)) {
if (playerEntity instanceof ServerPlayerEntity) {
Criteria.ITEM_USED_ON_BLOCK.trigger((ServerPlayerEntity)playerEntity, blockPos, itemStack);
}
if (blockState.isOf(Blocks.CALCITE) && itemStack.isOf(Items.POTION) && (PotionUtil.getPotion(itemStack) == Potions.HEALING || PotionUtil.getPotion(itemStack) == Potions.STRONG_HEALING)) {
playerEntity.setStackInHand(context.getHand(), ItemUsage.exchangeStack(itemStack, playerEntity, new ItemStack(Items.GLASS_BOTTLE)));
world.setBlockState(blockPos, ParadiseLostBlocks.BLOOMED_CALCITE.getDefaultState());
// particles
for (int i = 0; i < 16; i++) {
double xOffset = random.nextDouble();
double yOffset = random.nextDouble();
double zOffset = random.nextDouble();
world.addParticle(ParticleTypes.ENTITY_EFFECT, blockPos.getX() + xOffset, blockPos.getY() + yOffset, blockPos.getZ() + zOffset, 0.97, 0.15, 0.14);
}
BloomedCalciteUtil.applyHealing(playerEntity, world, blockPos, random, itemStack);
if (!world.isClient) {
// sound
world.playSound(null, blockPos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
cir.setReturnValue(ActionResult.success(world.isClient));
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/net/id/paradiselost/util/BloomedCalciteUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.id.paradiselost.util;

import net.id.paradiselost.blocks.ParadiseLostBlocks;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;

public class BloomedCalciteUtil {

public static void applyHealing(Entity applier, World world, BlockPos blockPos, Random random, ItemStack itemStack) {
if (applier instanceof ServerPlayerEntity) {
Criteria.ITEM_USED_ON_BLOCK.trigger((ServerPlayerEntity)applier, blockPos, itemStack);
}

world.setBlockState(blockPos, ParadiseLostBlocks.BLOOMED_CALCITE.getDefaultState());
// particles
for (int i = 0; i < 16; i++) {
double xOffset = random.nextDouble();
double yOffset = random.nextDouble();
double zOffset = random.nextDouble();
world.addParticle(ParticleTypes.ENTITY_EFFECT, blockPos.getX() + xOffset, blockPos.getY() + yOffset, blockPos.getZ() + zOffset, 0.97, 0.15, 0.14);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/paradise_lost.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"entity.EntityMixin",
"entity.LivingEntityMixin",
"entity.PiglinBrainMixin",
"entity.PotionEntityMixin",
"entity.PlayerEntityMixin",
"fluid.FlowableFluidMixin",
"item.ArmorMaterialsAccessor",
Expand Down

0 comments on commit c2104db

Please sign in to comment.