This commit is contained in:
yrsegal@gmail.com 2022-07-02 23:23:28 -04:00
parent 2be0021185
commit 1b01174186
8 changed files with 52 additions and 5 deletions

View file

@ -36,6 +36,9 @@ object OpBreakBlock : SpellAction {
return
val blockstate = ctx.world.getBlockState(pos)
if (!IXplatAbstractions.INSTANCE.isBreakingAllowed(ctx.world, pos, blockstate, ctx.caster))
return
val tier =
HexConfig.server().opBreakHarvestLevel()

View file

@ -52,6 +52,10 @@ class OpConjureBlock(val light: Boolean) : SpellAction {
val worldState = ctx.world.getBlockState(pos)
if (worldState.canBeReplaced(placeContext)) {
val block = if (this.light) HexBlocks.CONJURED_LIGHT else HexBlocks.CONJURED_BLOCK
if (!IXplatAbstractions.INSTANCE.isPlacingAllowed(ctx.world, pos, ItemStack(block), ctx.caster))
return
val state = block.getStateForPlacement(placeContext)
if (state != null) {
ctx.world.setBlock(pos, state, 5)

View file

@ -36,7 +36,7 @@ object OpCreateWater : SpellAction {
private data class Spell(val pos: BlockPos) : RenderedSpell {
override fun cast(ctx: CastingContext) {
if (!ctx.world.mayInteract(ctx.caster, pos))
if (!ctx.world.mayInteract(ctx.caster, pos) || !IXplatAbstractions.INSTANCE.isPlacingAllowed(ctx.world, pos, ItemStack(Items.WATER_BUCKET), ctx.caster))
return
val state = ctx.world.getBlockState(pos)

View file

@ -8,6 +8,7 @@ import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.getBlockPos
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.api.spell.mishaps.MishapBadBlock
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.core.particles.BlockParticleOption
import net.minecraft.core.particles.ParticleTypes
@ -64,6 +65,10 @@ object OpPlaceBlock : SpellAction {
val placeeSlot = ctx.getOperativeSlot { it.item is BlockItem }
if (placeeSlot != null) {
val placeeStack = ctx.caster.inventory.getItem(placeeSlot).copy()
if (!IXplatAbstractions.INSTANCE.isPlacingAllowed(ctx.world, pos, placeeStack, ctx.caster))
return
if (!placeeStack.isEmpty) {
// https://github.com/VazkiiMods/Psi/blob/master/src/main/java/vazkii/psi/common/spell/trick/block/PieceTrickPlaceBlock.java#L143
val oldStack = ctx.caster.getItemInHand(ctx.castingHand)

View file

@ -37,7 +37,7 @@ object OpCreateLava : SpellAction {
private data class Spell(val pos: BlockPos) : RenderedSpell {
override fun cast(ctx: CastingContext) {
if (!ctx.world.mayInteract(ctx.caster, pos))
if (!ctx.world.mayInteract(ctx.caster, pos) || !IXplatAbstractions.INSTANCE.isPlacingAllowed(ctx.world, pos, ItemStack(Items.LAVA_BUCKET), ctx.caster))
return
val state = ctx.world.getBlockState(pos)

View file

@ -147,6 +147,10 @@ public interface IXplatAbstractions {
Registry<IotaType<?>> getIotaTypeRegistry();
boolean isBreakingAllowed(Level world, BlockPos pos, BlockState state, Player player);
boolean isPlacingAllowed(Level world, BlockPos pos, ItemStack blockStack, Player player);
// interop
PehkuiInterop.ApiAbstraction getPehkuiApi();

View file

@ -27,6 +27,8 @@ import com.jamieswhiteshirt.reachentityattributes.ReachEntityAttributes;
import com.mojang.serialization.Lifecycle;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
@ -52,6 +54,7 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Mob;
@ -376,6 +379,20 @@ public class FabricXplatImpl implements IXplatAbstractions {
return IOTA_TYPE_REGISTRY;
}
@Override
public boolean isBreakingAllowed(Level world, BlockPos pos, BlockState state, Player player) {
return PlayerBlockBreakEvents.BEFORE.invoker().beforeBlockBreak(world, player, pos, state, world.getBlockEntity(pos));
}
@Override
public boolean isPlacingAllowed(Level world, BlockPos pos, ItemStack blockStack, Player player) {
ItemStack cached = player.getMainHandItem();
player.setItemInHand(InteractionHand.MAIN_HAND, blockStack.copy());
var success = UseItemCallback.EVENT.invoker().interact(player, world, InteractionHand.MAIN_HAND);
player.setItemInHand(InteractionHand.MAIN_HAND, cached);
return success.getResult() == InteractionResult.PASS; // No other mod tried to consume this
}
private static PehkuiInterop.ApiAbstraction PEHKUI_API = null;
@Override

View file

@ -61,12 +61,12 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.common.TierSortingRegistry;
import net.minecraftforge.common.ToolActions;
import net.minecraftforge.common.*;
import net.minecraftforge.common.loot.CanToolPerformAction;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
@ -419,6 +419,20 @@ public class ForgeXplatImpl implements IXplatAbstractions {
return IOTA_TYPE_REGISTRY;
}
@Override
public boolean isBreakingAllowed(Level world, BlockPos pos, BlockState state, Player player) {
return !MinecraftForge.EVENT_BUS.post(new BlockEvent.BreakEvent(world, pos, state, player));
}
@Override
public boolean isPlacingAllowed(Level world, BlockPos pos, ItemStack blockStack, Player player) {
ItemStack cached = player.getMainHandItem();
player.setItemInHand(InteractionHand.MAIN_HAND, blockStack.copy());
var evt = ForgeHooks.onRightClickBlock(player, InteractionHand.MAIN_HAND, pos, new BlockHitResult(Vec3.atCenterOf(pos), Direction.DOWN, pos, true));
player.setItemInHand(InteractionHand.MAIN_HAND, cached);
return !evt.isCanceled();
}
// it's literally the EXACT SAME on fabric aaa
private static PehkuiInterop.ApiAbstraction PEHKUI_API = null;