QωQ Library

QωQ Library

这是一个用于Fabric的模组,它提供了一种简单的方式来订阅和自定义事件!

基础库

issues forks stars license
QωQ Library
这是一个可以简单订阅事件的库。 它提供了一种注册对象(例如物品)的方式,示例如下:

public static final Registration<Item> ITEMS = new Registration<>(Registry.ITEM, MOD_ID);
public static final Item EXAMPLE_ITEM = ITEMS.register("example_item", () -> new Item(new Item.Settings()));

别忘了将其添加到你的主类中:

@Override
public void onInitialize() {
  ItemInit.ITEMS.register();
}

如果你想订阅一个事件,可以这样做:

@ModEvent
public class OnEventHandle {
    @SubscribeEvent
    public static void onEvent(IEvent event){
        //todo
    }
}

但是,你必须告诉程序你把类放在了哪里。
在这个示例中,就像这样:

@Override
public void onInitialize() {
  ItemInit.ITEMS.register();
  EventLoader.initEvent("org.abstruck.qwq.init.event");
}

但是,如果你把类放在 std.init.event 中,它会被直接订阅。
警告:不要将带有 @Mixin 的类放入此文件夹中!!!

也许有时你无法在库中找到想要的事件,你可以自定义一个:

public class AfterBreakEvent implements IEvent{
        private final World world;
        private final PlayerEntity player;
        private final BlockPos pos;
        private final BlockState state;
        private final BlockEntity blockEntity;
        private final ItemStack stack;
        public AfterBreakEvent(World world, PlayerEntity player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack stack) {
            this.world = world;
            this.player = player;
            this.pos = pos;
            this.state = state;
            this.blockEntity = blockEntity;
            this.stack = stack;
        }

        public PlayerEntity getPlayer() {
            return player;
        }

        public BlockPos getPos() {
            return pos;
        }
        
        public BlockState getState(){
            reutrn state;
        }

        public World getWorld() {
            return world;
        }

        public BlockEntity getBlockEntity() {
            return blockEntity;
        }

        public ItemStack getStack() {
            return stack;
        }
    }

并且别忘了编写 mixin:

@Mixin(Block.class)
public abstract class BlockMixin {
    @Inject(method = "afterBreak", at = @At("RETURN"))
    public void afterBreak(World world, PlayerEntity player, BlockPos pos, BlockState state, BlockEntity blockEntity, ItemStack stack, CallbackInfo ci){
        EventManager.onEventAction(() -> new AfterBreakEvent(world, player, pos, state, blockEntity, stack));
    }
}