You can no longer place spellbooks in occupied blocks. Fixes #451

This commit is contained in:
Sollace 2024-09-16 14:52:00 +01:00
parent 5b3a8bee95
commit fab6e7b3dd
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB

View file

@ -20,6 +20,8 @@ import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPointer; import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Position;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
public class SpellbookItem extends BookItem implements Dispensable { public class SpellbookItem extends BookItem implements Dispensable {
@ -29,17 +31,20 @@ public class SpellbookItem extends BookItem implements Dispensable {
} }
@Override @Override
public TypedActionResult<ItemStack> dispenseStack(BlockPointer source, ItemStack stack) { public TypedActionResult<ItemStack> dispenseStack(BlockPointer pointer, ItemStack stack) {
Direction facing = source.getBlockState().get(DispenserBlock.FACING); Direction facing = pointer.getBlockState().get(DispenserBlock.FACING);
BlockPos pos = source.getPos().offset(facing); Position pos = DispenserBlock.getOutputLocation(pointer);
float yaw = facing.getOpposite().asRotation(); float yaw = facing.getOpposite().asRotation();
placeBook(stack, source.getWorld(), pos.getX(), pos.getY(), pos.getZ(), yaw, null); if (placeBook(stack, pointer.getWorld(), pos, yaw, null)) {
stack.decrement(1); stack.decrement(1);
return new TypedActionResult<>(ActionResult.SUCCESS, stack); return new TypedActionResult<>(ActionResult.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.FAIL, stack);
}
@Override @Override
public ActionResult useOnBlock(ItemUsageContext context) { public ActionResult useOnBlock(ItemUsageContext context) {
@ -49,24 +54,28 @@ public class SpellbookItem extends BookItem implements Dispensable {
if (!context.getWorld().isClient) { if (!context.getWorld().isClient) {
BlockPos pos = context.getBlockPos().offset(context.getSide()); BlockPos pos = context.getBlockPos().offset(context.getSide());
placeBook(context.getStack(), context.getWorld(), pos.getX(), pos.getY(), pos.getZ(), context.getPlayerYaw() + 180, player); if (placeBook(context.getStack(), context.getWorld(), Vec3d.ofBottomCenter(pos), context.getPlayerYaw() + 180, player)) {
if (!player.getAbilities().creativeMode) { if (!player.getAbilities().creativeMode) {
player.getStackInHand(context.getHand()).decrement(1); player.getStackInHand(context.getHand()).decrement(1);
} }
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
}
return ActionResult.PASS; return ActionResult.PASS;
} }
private static void placeBook(ItemStack stack, World world, int x, int y, int z, float yaw, @Nullable Entity placer) { private static boolean placeBook(ItemStack stack, World world, Position pos, float yaw, @Nullable Entity placer) {
SpellbookEntity book = UEntities.SPELLBOOK.create(world); SpellbookEntity book = UEntities.SPELLBOOK.create(world);
book.refreshPositionAndAngles(x + 0.5, y, z + 0.5, 0, 0); book.refreshPositionAndAngles(pos.getX(), pos.getY(), pos.getZ(), 0, 0);
book.setHeadYaw(yaw); book.setHeadYaw(yaw);
book.setYaw(yaw); book.setYaw(yaw);
if (!book.canSpawn(world)) {
return false;
}
@Nullable @Nullable
NbtCompound tag = stack.getSubNbt("spellbookState"); NbtCompound tag = stack.getSubNbt("spellbookState");
if (tag != null) { if (tag != null) {
@ -80,6 +89,8 @@ public class SpellbookItem extends BookItem implements Dispensable {
altar.generateDecorations(world); altar.generateDecorations(world);
UCriteria.LIGHT_ALTAR.trigger(placer); UCriteria.LIGHT_ALTAR.trigger(placer);
}); });
return true;
} }
} }