rewrite some interaction handling and to use some event calls instead of mixin. should cover ae2 wrenches now close #106

This commit is contained in:
Flemmli97 2021-12-21 17:03:53 +01:00
parent be4099fa54
commit 5dc0584a7a
10 changed files with 32 additions and 47 deletions

View File

@ -5,8 +5,9 @@ Flan 1.6.5
- New config "customItemPermission" and "customBlockPermission"
allows to specify a mapping of item/block to permission
example is endcrystals are mapped to the ENDCRYSTALPLACE permission internally
Syntax is <item/block>-<permission>
Syntax is <item/block>-\<permission>
Use @ to indicate a tag
- Moved some interaction checks to a different location so more mods should be covered by default
Flan 1.6.4
======================

View File

@ -70,7 +70,9 @@ public class Config {
);
public List<String> itemPermission = Lists.newArrayList(
"@c:wrenches-INTERACTBLOCK"
"@c:wrenches-INTERACTBLOCK",
"appliedenergistics2:nether_quartz_wrench-INTERACTBLOCK",
"appliedenergistics2:certus_quartz_wrench-INTERACTBLOCK"
);
public List<String> blockPermission = Lists.newArrayList(
);

View File

@ -24,6 +24,7 @@ import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.level.block.LecternBlock;
@ -101,7 +102,7 @@ public class BlockInteractEvents {
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.blockPosition().getY());
return InteractionResult.FAIL;
}
if (blockEntity != null) {
if (blockEntity != null && !player.isSecondaryUseActive() && !stack.isEmpty()) {
if (blockEntity instanceof LecternBlockEntity) {
if (claim.canInteract(player, PermissionRegistry.LECTERNTAKE, hitResult.getBlockPos(), false))
return InteractionResult.PASS;
@ -116,7 +117,13 @@ public class BlockInteractEvents {
return InteractionResult.FAIL;
}
}
return claim.canInteract(player, PermissionRegistry.INTERACTBLOCK, hitResult.getBlockPos(), false) ? InteractionResult.PASS : InteractionResult.FAIL;
InteractionResult res = ItemInteractEvents.onItemUseBlock(new UseOnContext(player, hand, hitResult));
if (claim.canInteract(player, PermissionRegistry.INTERACTBLOCK, hitResult.getBlockPos(), false) || res == InteractionResult.FAIL) {
if (res == InteractionResult.FAIL)
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.blockPosition().getY());
return res;
}
return InteractionResult.PASS;
}
return InteractionResult.PASS;
}

View File

@ -2,20 +2,15 @@ package io.github.flemmli97.flan.mixin;
import io.github.flemmli97.flan.event.BlockInteractEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(BlockBehaviour.BlockStateBase.class)
public abstract class AbstractBlockStateMixin {
@ -27,19 +22,6 @@ public abstract class AbstractBlockStateMixin {
}
}
/**
* Can't use the hooks from both fabric or forge cause they are too generic.
* Wouldn't be able to place blocks after cancelling them
*/
@Inject(method = "use", at = @At(value = "HEAD"), cancellable = true)
private void useBlock(Level world, Player player, InteractionHand hand, BlockHitResult result, CallbackInfoReturnable<InteractionResult> info) {
InteractionResult res = BlockInteractEvents.useBlocks(player, world, hand, result);
if (res != InteractionResult.PASS) {
info.setReturnValue(res);
info.cancel();
}
}
@Shadow
protected abstract BlockState asState();
}

View File

@ -1,23 +0,0 @@
package io.github.flemmli97.flan.mixin;
import io.github.flemmli97.flan.event.ItemInteractEvents;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
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.CallbackInfoReturnable;
@Mixin(ItemStack.class)
public abstract class ItemStackMixin {
@Inject(method = "useOn", at = @At(value = "HEAD"), cancellable = true)
private void blockUse(UseOnContext context, CallbackInfoReturnable<InteractionResult> info) {
InteractionResult result = ItemInteractEvents.onItemUseBlock(context);
if (result != InteractionResult.PASS) {
info.setReturnValue(result);
info.cancel();
}
}
}

View File

@ -10,7 +10,6 @@
"EntityMixin",
"TurtleEggMixin",
"IPersistentProjectileVars",
"ItemStackMixin",
"ILecternBlockValues",
"FluidMixin",
"RaidManagerMixin",

View File

@ -13,23 +13,30 @@ import io.github.flemmli97.flan.player.PlayerDataHandler;
import io.github.flemmli97.flan.scoreboard.ClaimCriterias;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
public class FlanFabric implements ModInitializer {
public static final ResourceLocation EventPhase = new ResourceLocation("flan", "events");
@Override
public void onInitialize() {
PlayerBlockBreakEvents.BEFORE.register(BlockInteractEvents::breakBlocks);
AttackBlockCallback.EVENT.register(BlockInteractEvents::startBreakBlocks);
UseBlockCallback.EVENT.addPhaseOrdering(EventPhase, Event.DEFAULT_PHASE);
UseBlockCallback.EVENT.register(EventPhase, BlockInteractEvents::useBlocks);
UseEntityCallback.EVENT.register(EntityInteractEvents::useAtEntity);
AttackEntityCallback.EVENT.register(EntityInteractEvents::attackEntity);
UseItemCallback.EVENT.register(ItemInteractEvents::useItem);

View File

@ -25,7 +25,7 @@
],
"depends": {
"fabricloader": ">=0.7.4",
"fabric": ">=0.19.0"
"fabric": ">=0.42.0"
},
"suggests": {
"flamingo": "*"

View File

@ -8,6 +8,7 @@ import io.github.flemmli97.flan.forgeevent.ServerEvents;
import io.github.flemmli97.flan.forgeevent.WorldEventsForge;
import io.github.flemmli97.flan.scoreboard.ClaimCriterias;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
@ -29,6 +30,7 @@ public class FlanForge {
forge.addListener(ItemInteractEventsForge::useItem);
forge.addListener(BlockInteractEventsForge::startBreakBlocks);
forge.addListener(BlockInteractEventsForge::breakBlocks);
forge.addListener(EventPriority.HIGHEST, BlockInteractEventsForge::useBlocks);
forge.addListener(EntityInteractEventsForge::attackEntity);
forge.addListener(EntityInteractEventsForge::useAtEntity);
forge.addListener(EntityInteractEventsForge::useEntity);

View File

@ -22,4 +22,12 @@ public class BlockInteractEventsForge {
if (!BlockInteractEvents.breakBlocks((Level) event.getWorld(), event.getPlayer(), event.getPos(), event.getState(), event.getWorld().getBlockEntity(event.getPos())))
event.setCanceled(true);
}
public static void useBlocks(PlayerInteractEvent.RightClickBlock event) {
InteractionResult res = BlockInteractEvents.useBlocks(event.getPlayer(), event.getWorld(), event.getHand(), event.getHitVec());
if (res != InteractionResult.PASS) {
event.setCancellationResult(res);
event.setCanceled(true);
}
}
}