Skip to content

Commit

Permalink
fix: Change instances of caster.level to focalPoint.level
Browse files Browse the repository at this point in the history
Fixes #705
Also replaces instances of `level.hasChunkAt` to the non-deprecated version `level.hasChunk`
  • Loading branch information
Kamefrede committed Nov 7, 2023
1 parent e532fc2 commit 0a192b4
Show file tree
Hide file tree
Showing 20 changed files with 36 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
BlockState state = Blocks.COMPARATOR.defaultBlockState()
.setValue(HorizontalDirectionalBlock.FACING, whichWay.getOpposite());

return ((ComparatorBlock) Blocks.COMPARATOR).getInputSignal(context.caster.level, pos.relative(whichWay), state) * 1.0;
return ((ComparatorBlock) Blocks.COMPARATOR).getInputSignal(context.focalPoint.level, pos.relative(whichWay), state) * 1.0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void initParams() {
@Override
public Object execute(SpellContext context) throws SpellRuntimeException {
BlockPos pos = SpellHelpers.getBlockPos(this, context, target, false, false);
BlockState state = context.caster.level.getBlockState(pos);
return state.getDestroySpeed(context.caster.level, pos) * 1.0D;
BlockState state = context.focalPoint.level.getBlockState(pos);
return state.getDestroySpeed(context.focalPoint.level, pos) * 1.0D;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void initParams() {
@Override
public Object execute(SpellContext context) throws SpellRuntimeException {
BlockPos pos = SpellHelpers.getBlockPos(this, context, target, false, false);
int j = context.caster.level.getMaxLocalRawBrightness(pos);
int j = context.focalPoint.level.getMaxLocalRawBrightness(pos);
return j * 1.0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
package vazkii.psi.common.spell.operator.block;

import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;

import vazkii.psi.api.PsiAPI;
import vazkii.psi.api.internal.Vector3;
import vazkii.psi.api.spell.Spell;
import vazkii.psi.api.spell.SpellContext;
Expand All @@ -39,11 +37,8 @@ public void initParams() {
@Override
public Object execute(SpellContext context) throws SpellRuntimeException {
BlockPos pos = SpellHelpers.getBlockPos(this, context, position, false, false);
BlockState state = context.caster.level.getBlockState(pos);
ItemStack tool = context.tool;
if(tool.isEmpty()) {
tool = PsiAPI.getPlayerCAD(context.caster);
}
BlockState state = context.focalPoint.level.getBlockState(pos);

//TODO Fix low mining level items returning 1
return PieceTrickBreakBlock.getHarvestLevel(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
BlockPos pos = SpellHelpers.getBlockPos(this, context, target, false, false);
Direction facing = SpellHelpers.getFacing(this, context, axisParam);

BlockState state = context.caster.level.getBlockState(pos);
return state.isFaceSturdy(context.caster.level, pos, facing) ? 1.0D : 0.D;
BlockState state = context.focalPoint.level.getBlockState(pos);
return state.isFaceSturdy(context.focalPoint.level, pos, facing) ? 1.0D : 0.D;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {

double maxLen = SpellHelpers.rangeLimitParam(this, context, max, SpellContext.MAX_DISTANCE);

Entity entity = rayTraceEntities(context.caster.level, context.caster, originVal.toVec3D(), rayVal.toVec3D(),
Entity entity = rayTraceEntities(context.focalPoint.level, context.caster, originVal.toVec3D(), rayVal.toVec3D(),
pred -> !pred.isSpectator() && pred.isAlive() && pred.isPickable() && !(pred instanceof ISpellImmune), maxLen);
if(entity == null) {
throw new SpellRuntimeException(SpellRuntimeException.NULL_TARGET);
Expand All @@ -66,8 +66,8 @@ public Object execute(SpellContext context) throws SpellRuntimeException {

/**
* [VanillaCopy]
* {@link net.minecraft.entity.projectile.ProjectileHelper#rayTraceEntities(World, Entity, Vector3d, Vector3d, AxisAlignedBB, Predicate)}
* (World, Entity, Vector3d, Vector3d, AxisAlignedBB, Predicate, double)}
* {@link net.minecraft.world.entity.projectile.ProjectileUtil#getEntityHitResult(Entity, Vec3, Vec3, AABB, Predicate, double)}
* (World, Entity, Vec3, Vec3, AxisAlignedBB, Predicate, double)}
* Some slight tweaks as we don't need an AABB provided to us, we can just make one.
*/
public static Entity rayTraceEntities(Level world, Entity caster, Vec3 positionVector, Vec3 lookVector, Predicate<Entity> predicate, double maxDistance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
double radiusVal = Math.min(MAX_DISTANCE, this.getNonnullParamValue(context, radius).doubleValue());

if(radiusVal == 0.0) {
IDetonationHandler.performDetonation(context.caster.level, context.caster, 0, entity -> entity == context.caster);
IDetonationHandler.performDetonation(context.focalPoint.level, context.caster, 0, entity -> entity == context.caster);
return null;
}
IDetonationHandler.performDetonation(context.caster.level, context.caster, context.focalPoint, radiusVal);
IDetonationHandler.performDetonation(context.focalPoint.level, context.caster, context.focalPoint, radiusVal);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void addToMetadata(SpellMetadata meta) throws SpellCompilationException {
@Override
public Object execute(SpellContext context) throws SpellRuntimeException {
BlockPos pos = SpellHelpers.getBlockPos(this, context, position, true, false);
return bonemeal(context.caster, context.caster.level, pos);
return bonemeal(context.caster, context.focalPoint.level, pos);
}

public InteractionResult bonemeal(Player player, Level world, BlockPos pos) {
if(!world.hasChunkAt(pos) || !world.mayInteract(player, pos)) {
if(!world.hasChunk(pos.getX(), pos.getY()) || !world.mayInteract(player, pos)) {
return InteractionResult.PASS;
}
BlockHitResult hit = new BlockHitResult(Vec3.ZERO, Direction.UP, pos, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
int instrumentId = Mth.clamp((int) instrumentVal, 0, Psi.noteblockSoundEvents.size() - 1);

float f = (float) Math.pow(2, (pitchVal - 12) / 12.0);
context.caster.level.playSound(null, pos, Psi.noteblockSoundEvents.get(instrumentId), SoundSource.RECORDS, (float) volVal, f);
context.focalPoint.level.playSound(null, pos, Psi.noteblockSoundEvents.get(instrumentId), SoundSource.RECORDS, (float) volVal, f);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
}

if(context.focalPoint.getCommandSenderWorld() instanceof ServerLevel) {
LightningBolt lightning = new LightningBolt(EntityType.LIGHTNING_BOLT, context.caster.level);
LightningBolt lightning = new LightningBolt(EntityType.LIGHTNING_BOLT, context.focalPoint.level);
lightning.setPosRaw(positionVal.x, positionVal.y, positionVal.z);
((ServerLevel) context.focalPoint.getCommandSenderWorld()).addFreshEntity(lightning);
context.focalPoint.getCommandSenderWorld().addFreshEntity(lightning);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
if(placeEvent.isCanceled()) {
return null;
}
return placeWater(context.caster, context.caster.level, pos);
return placeWater(context.caster, context.focalPoint.level, pos);
}

// [VanillaCopy] BucketItem.tryPlaceContainingLiquid because buckets are dumb
public static boolean placeWater(@Nullable Player playerIn, Level worldIn, BlockPos pos) {
if(!worldIn.hasChunkAt(pos) || !worldIn.mayInteract(playerIn, pos)) {
if(playerIn == null || !worldIn.hasChunk(pos.getX(), pos.getY()) || !worldIn.mayInteract(playerIn, pos)) {
return false;
}
BlockState blockstate = worldIn.getBlockState(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -93,7 +92,7 @@ public static void removeBlockWithDrops(SpellContext context, Player player, Lev
stack = PsiAPI.getPlayerCAD(player);
}

if(!world.hasChunkAt(pos)) {
if(!world.hasChunk(pos.getX(), pos.getY())) {
return;
}

Expand Down Expand Up @@ -123,8 +122,8 @@ public static BreakEvent createBreakEvent(BlockState state, Player player, Level
{
event.setExpToDrop(0);
} else {
int fortuneLevel = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, tool);
int silkLevel = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool);
int fortuneLevel = tool.getEnchantmentLevel(Enchantments.BLOCK_FORTUNE);
int silkLevel = tool.getEnchantmentLevel(Enchantments.SILK_TOUCH);
event.setExpToDrop(state.getExpDrop(world, RandomSource.create(), pos, fortuneLevel, silkLevel));
}
return event;
Expand All @@ -133,7 +132,6 @@ public static BreakEvent createBreakEvent(BlockState state, Player player, Level
/**
* Item stack aware harvest check
* Also sets global state {@link PieceTrickBreakBlock#doingHarvestCheck} to true during the check
*
* //@see IForgeBlockState#canHarvestBlock(IBlockReader, BlockPos, PlayerEntity)
*/
public static boolean canHarvestBlock(BlockState state, Player player, Level world, BlockPos pos, ItemStack stack) {
Expand Down Expand Up @@ -174,10 +172,6 @@ public static boolean canHarvest(int harvestLevel, BlockState state) {
return !getTool(harvestLevel, state).isEmpty();
}

public static ItemStack getHarvestToolStack(int harvestLevel, BlockState state) {
return getTool(harvestLevel, state).copy();
}

private static ItemStack getTool(int harvestLevel, BlockState state) {
if(!state.requiresCorrectToolForDrops()) {
return HARVEST_TOOLS_BY_LEVEL.get(0).get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
tool = PsiAPI.getPlayerCAD(context.caster);
}

Level world = context.caster.level;
Level world = context.focalPoint.level;
Vector3 targetNorm = targetVal.copy().normalize();
for(BlockPos blockPos : MathHelper.getBlocksAlongRay(positionVal.toVec3D(), positionVal.copy().add(targetNorm.copy().multiply(maxBlocksInt)).toVec3D(), maxBlocksInt)) {
if(!context.isInRadius(Vector3.fromBlockPos(blockPos))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static void conjure(SpellContext context, @Nullable Number timeVal, Block
}

public static boolean conjure(Level world, BlockPos pos, Player player, BlockState state) {
if(!world.hasChunkAt(pos) || !world.mayInteract(player, pos)) {
if(!world.hasChunk(pos.getX(), pos.getY()) || !world.mayInteract(player, pos)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
Vector3 positionVal = SpellHelpers.getVector3(this, context, position, true, false);
Vector3 targetVal = SpellHelpers.getVector3(this, context, target, false, false);
int maxBlocksVal = this.getParamValue(context, maxBlocks).intValue();
Level world = context.caster.level;
Level world = context.focalPoint.level;

Map<BlockPos, BlockState> toSet = new HashMap<>();
Map<BlockPos, BlockState> toRemove = new HashMap<>();
Expand All @@ -83,7 +83,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
LinkedHashSet<BlockPos> moveableBlocks = new LinkedHashSet<>();
LinkedHashSet<BlockPos> immovableBlocks = new LinkedHashSet<>();

/**
/*
* TODO: Find a better solution than this bandaid for block duping (see #740)
* A possible solution is moving this logic to {@link PieceTrickBreakBlock}
* As well as passing the spell context to it as a parameter. The Spell Context would need to have a way to
Expand Down Expand Up @@ -159,12 +159,12 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
}

for(Map.Entry<BlockPos, BlockState> pairtoRemove : toRemove.entrySet()) {
context.caster.level.removeBlock(pairtoRemove.getKey(), true);
context.caster.level.levelEvent(2001, pairtoRemove.getKey(), Block.getId(pairtoRemove.getValue()));
context.focalPoint.level.removeBlock(pairtoRemove.getKey(), true);
context.focalPoint.level.levelEvent(2001, pairtoRemove.getKey(), Block.getId(pairtoRemove.getValue()));
}

for(Map.Entry<BlockPos, BlockState> pairToSet : toSet.entrySet()) {
context.caster.level.setBlockAndUpdate(pairToSet.getKey(), pairToSet.getValue());
context.focalPoint.level.setBlockAndUpdate(pairToSet.getKey(), pairToSet.getValue());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void placeBlock(Player player, Level world, BlockPos pos, int slot
}

public static void placeBlock(Player player, Level world, BlockPos pos, int slot, boolean particles, boolean conjure, Direction direction, Direction horizontalDirection) {
if(!world.hasChunkAt(pos) || !world.mayInteract(player, pos)) {
if(!world.hasChunk(pos.getX(), pos.getY()) || !world.mayInteract(player, pos)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void addToMetadata(SpellMetadata meta) throws SpellCompilationException {
}

@Override
@SuppressWarnings("deprecation")
public Object execute(SpellContext context) throws SpellRuntimeException {
Vector3 positionVal = this.getParamValue(context, position);

Expand All @@ -72,7 +71,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
BlockState state = context.focalPoint.getCommandSenderWorld().getBlockState(pos);
Block block = state.getBlock();
ItemStack stack = new ItemStack(block);
BlockEvent.BreakEvent event = PieceTrickBreakBlock.createBreakEvent(state, context.caster, context.caster.level, pos, tool);
BlockEvent.BreakEvent event = PieceTrickBreakBlock.createBreakEvent(state, context.caster, context.focalPoint.level, pos, tool);
MinecraftForge.EVENT_BUS.post(event);
if(event.isCanceled()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
BlockState state = context.focalPoint.getCommandSenderWorld().getBlockState(blockPos);
Block block = state.getBlock();
ItemStack stack = new ItemStack(block);
BlockEvent.BreakEvent event = PieceTrickBreakBlock.createBreakEvent(state, context.caster, context.caster.level, blockPos, tool);
BlockEvent.BreakEvent event = PieceTrickBreakBlock.createBreakEvent(state, context.caster, context.focalPoint.level, blockPos, tool);
MinecraftForge.EVENT_BUS.post(event);
if(event.isCanceled()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public PieceTrickTill(Spell spell) {
}

public static InteractionResult tillBlock(Player player, Level world, BlockPos pos) {
if(!world.hasChunkAt(pos) || !world.mayInteract(player, pos)) {
if(!world.hasChunk(pos.getX(), pos.getY()) || !world.mayInteract(player, pos)) {
return InteractionResult.PASS;
}
BlockHitResult hit = new BlockHitResult(Vec3.ZERO, Direction.UP, pos, false);
Expand All @@ -70,7 +70,7 @@ public void addToMetadata(SpellMetadata meta) throws SpellCompilationException {
public Object execute(SpellContext context) throws SpellRuntimeException {
BlockPos pos = SpellHelpers.getBlockPos(this, context, position, true, false);

return tillBlock(context.caster, context.caster.level, pos);
return tillBlock(context.caster, context.focalPoint.level, pos);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Object execute(SpellContext context) throws SpellRuntimeException {
for(BlockPos blockPos : MathHelper.getBlocksAlongRay(positionVal.toVec3D(), positionVal.copy().add(targetNorm.copy().multiply(maxBlocksInt)).toVec3D(), maxBlocksInt)) {

if(SpellHelpers.isBlockPosInRadius(context, blockPos)) {
PieceTrickTill.tillBlock(context.caster, context.caster.level, blockPos);
PieceTrickTill.tillBlock(context.caster, context.focalPoint.level, blockPos);
}

}
Expand Down

0 comments on commit 0a192b4

Please sign in to comment.