port of 1b01174186
This commit is contained in:
yrsegal@gmail.com 2022-07-24 20:07:09 -04:00
parent 25b594d75a
commit 549d6f685c
8 changed files with 52 additions and 11 deletions

View file

@ -35,6 +35,9 @@ object OpBreakBlock : SpellOperator {
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,9 @@ class OpConjure(val light: Boolean) : SpellOperator {
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

@ -35,7 +35,7 @@ object OpCreateWater : SpellOperator {
override fun cast(ctx: CastingContext) {
val pos = BlockPos(target)
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

@ -4,6 +4,7 @@ import at.petrak.hexcasting.api.misc.ManaConstants
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
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,9 @@ object OpPlaceBlock : SpellOperator {
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

@ -2,11 +2,7 @@ package at.petrak.hexcasting.common.casting.operators.spells.great
import at.petrak.hexcasting.api.HexAPI
import at.petrak.hexcasting.api.misc.ManaConstants
import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.ParticleSpray
import at.petrak.hexcasting.api.spell.RenderedSpell
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.SpellOperator
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
@ -39,7 +35,7 @@ object OpCreateLava : SpellOperator {
override fun cast(ctx: CastingContext) {
val pos = BlockPos(target)
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

@ -143,6 +143,10 @@ public interface IXplatAbstractions {
String getModName(String namespace);
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

@ -24,6 +24,8 @@ import at.petrak.hexcasting.xplat.Platform;
import com.jamieswhiteshirt.reachentityattributes.ReachEntityAttributes;
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.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
@ -46,6 +48,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;
@ -357,6 +360,20 @@ public class FabricXplatImpl implements IXplatAbstractions {
return namespace;
}
@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

@ -58,17 +58,17 @@ 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;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.network.NetworkDirection;
@ -402,6 +402,20 @@ public class ForgeXplatImpl implements IXplatAbstractions {
return namespace;
}
@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;