Event 中文直翻「事件」,即是遊戲中的所有事件,通常搭配 Mixin 做偵測,並做出一些反應。例如:拿剪刀剪羊毛掉出鑽石、把物品丟到水裡會爆炸…等
此 interface 需要有 Event 的實作以及回應函數
public interface ItemEntityInWaterCallBack {
Event<ItemEntityInWaterCallBack> EVENT = EventFactory.createArrayBacked(ItemEntityInWaterCallBack.class, listeners -> itemEntity -> {
for (ItemEntityInWaterCallBack listener : listeners) {
ActionResult result = listener.interact(itemEntity);
if(result != ActionResult.PASS)
return result;
}
return ActionResult.PASS;
});
ActionResult interact(ItemEntity itemEntity);
}
ItemEntity
,因此在 interact()
函數中加入 ItemEntity
的參數
@Mixin(ItemEntity.class)
public abstract class ItemEntityTickMixin extends Entity {
public ItemEntityTickMixin(EntityType<?> type, World world) {
super(type, world);
}
@Shadow public abstract ItemEntity copy();
@Inject(method = "tick", at = @At(value = "HEAD"), cancellable = true)
public void tick(CallbackInfo info) {
ItemEntityInWaterCallBack.EVENT.invoker().interact(copy());
}
}
@Shadow
,請將 class 及函數加上 abstract 修飾
public class ModEvents {
public static void registerEvents() {
ItemEntityInWaterCallBack.EVENT.register(itemEntity -> {
World world = itemEntity.getWorld();
if (!world.isClient() && itemEntity.getStack().getItem() == Items.DIAMOND
&& world.getBlockState(new BlockPos(itemEntity.getPos())).getBlock() == Blocks.WATER) {
float power = 1 + Math.floorDiv(itemEntity.getStack().getCount(), 8);
world.createExplosion(itemEntity, DamageSource.explosion(new Explosion(world, itemEntity,
itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(), power)),
new ExplosionBehavior(), itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(),
power, false, Explosion.DestructionType.BREAK);
((ServerWorld) world).spawnParticles(ParticleTypes.EXPLOSION, itemEntity.getX(),
itemEntity.getY(), itemEntity.getZ(), (int) power, 2, 2, 2, 1);
itemEntity.kill();
return ActionResult.SUCCESS;
}
else return ActionResult.FAIL;
});
}
}
最後把函數放到主程式就完成囉!
ModEvents.registerEvents();
Minecraft Fabric Modding
(188){8}2h[4:7]/5h[8:1],6,5h[8:1],6,5h[8:1],6,7h[8:1],5h[8:1],6,2h[4:7]/5h[8:1],6,5h[8:1],6,5h[8:1],6,6,7,5,6,2h[4:7]/5h[8:1],6,5h[8:1],6,5h[8:1],6,7h[8:1],5h[8:1],6,2h[4:7]/5h[8:1],6,5h[8:1],6,5h[8:1],6,8-5[8:1],
Jul 7, 2023記得使用 Minecraft 1.18.2 版本!
Mar 15, 2023Java是一種廣泛使用的電腦程式語言,擁有跨平台、物件導向、泛型程式設計的特性,廣泛應用於企業級Web應用開發和移動應用開發。--《維基百科》 以下皆使用 IntelliJ 做示範 主函數 首先,創建一個 java 檔,IntelliJ 會自動幫你生成一個 class (必須與檔名相同) 接著打上 main,你就會看到自動完成,直接按 enter 即可。 完成後大概長這樣:
Feb 23, 2023指令的創建共分為三個部分:Mixin + DataSaver、Command、Events Mixin + Data Saver 在 mixin 資料夾中加入 ModEntityDataSaver.java: @Mixin(Entity.class) public abstract class ModEntityDataSaver implements IEntityDataSaver { private NbtCompound persistentData; @Override
Sep 19, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up