Minecraft 1.12.2模组开发(四十) buff效果(Potion Effect)
今天我们在1.12.2的模组中尝试实现几种特殊的buff效果
1.在开发包中新建potion包 -> potion包中新建一个BaseSimplePotion类作为我们药水效果的基类:
BaseSimplePotion.java
package com.joy187.rejoymod.potion.buff;import com.joy187.rejoymod.potion.ModPotions;
import com.joy187.rejoymod.util.Reference;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.init.PotionTypes;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionType;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;public class BaseSimplePotion extends Potion {//确定所有效果贴图的位置protected static final ResourceLocation resource = new ResourceLocation(Reference.Mod_ID,"textures/misc/potions.png");protected final int iconIndex;PotionType potionType=PotionTypes.MUNDANE;// if (!this.world.isRemote)
// {
// layer.getPotion().applyAttributesModifiersToEntity(this, this.getAttributeMap(), layer.getAmplifier());
// }public PotionType getPotionType() {return potionType;}//参数:是否为debuff buff粒子颜色 buff名称 buff对应的图标public BaseSimplePotion(boolean isBadEffectIn, int liquidColorIn, String name, int icon) {super(isBadEffectIn, liquidColorIn);setRegistryName(new ResourceLocation(Reference.Mod_ID, name));setPotionName("rejoymod.potion." + name);iconIndex = icon;ModPotions.INSTANCES.add(this);//setCreativeTab(tabs);}@SideOnly(Side.CLIENT)protected void render(int x, int y, float alpha) {Minecraft.getMinecraft().renderEngine.bindTexture(resource);Tessellator tessellator = Tessellator.getInstance();BufferBuilder buf = tessellator.getBuffer();buf.begin(7, DefaultVertexFormats.POSITION_TEX);GlStateManager.color(1, 1, 1, alpha);int textureX = iconIndex % 14 * 18;int textureY = 198 - iconIndex / 14 * 18;buf.pos(x, y + 18, 0).tex(textureX * 0.00390625, (textureY + 18) * 0.00390625).endVertex();buf.pos(x + 18, y + 18, 0).tex((textureX + 18) * 0.00390625, (textureY + 18) * 0.00390625).endVertex();buf.pos(x + 18, y, 0).tex((textureX + 18) * 0.00390625, textureY * 0.00390625).endVertex();buf.pos(x, y, 0).tex(textureX * 0.00390625, textureY * 0.00390625).endVertex();tessellator.draw();}//在我们的个人界面显示药水效果UI@Override@SideOnly(Side.CLIENT)public void renderInventoryEffect(int x, int y, PotionEffect effect, Minecraft mc) {render(x + 6, y + 7, 1);}@Override@SideOnly(Side.CLIENT)public void renderHUDEffect(int x, int y, PotionEffect effect, Minecraft mc, float alpha) {render(x + 3, y + 3, alpha);}
}
2.在potion包中定义一个我们的效果类PotionZenHeart,让其继承我们的效果基类:
PotionZenHeart.java
package com.joy187.rejoymod.potion.buff;import com.joy187.rejoymod.init.ModParticles;
import com.joy187.rejoymod.potion.ModPotions;
import com.joy187.rejoymod.util.EntityUtil;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Random;public class PotionZenHeart extends BasePotion {public PotionZenHeart(boolean isBadEffectIn, int liquidColorIn, String name, int icon) {super(isBadEffectIn, liquidColorIn, name, icon);resistancePerLevel = 0.25f;}//我们药水的实际效果@Overridepublic void performEffect(@Nonnull EntityLivingBase living, int amplified) {Random ran = new Random();int co = ran.nextInt(5);//回血效果if(co==4) living.heal(ran.nextFloat());}//当生物受伤,就减少其受到的魔法伤害@SubscribeEventpublic static void onCreatureHurt(LivingHurtEvent event) {World world = event.getEntity().getEntityWorld();EntityLivingBase hurtOne = event.getEntityLiving();if (event.isCanceled() || !event.getSource().isMagicDamage()){return;}//魔法减伤Collection<PotionEffect> activePotionEffects = hurtOne.getActivePotionEffects();for (int i = 0; i < activePotionEffects.size(); i++) {PotionEffect buff = (PotionEffect)activePotionEffects.toArray()[i];if (buff.getPotion() instanceof PotionZenHeart){PotionZenHeart modBuff = (PotionZenHeart)buff.getPotion();if (!world.isRemote){float reduceRatio = modBuff.resistancePerLevel * (buff.getAmplifier());event.setAmount(Math.max(1 - reduceRatio, 0f) * event.getAmount());}}}}
}
3.在potion包中新建一个ModPotions类,用于将我们所有的药水效果进行注册:
ModPotions.java
package com.joy187.rejoymod.potion;import com.joy187.rejoymod.IdlFramework;
import com.joy187.rejoymod.init.InitPotionTypes;
import com.joy187.rejoymod.potion.buff.*;
import com.joy187.rejoymod.util.Reference;
import net.minecraft.init.MobEffects;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionType;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;@Mod.EventBusSubscriber(modid = Reference.Mod_ID)
public class ModPotions {public static final List<Potion> INSTANCES = new ArrayList<Potion>();public static final List<PotionType> TYPE_INSTANCES = new ArrayList<>();//将模组中所有的药水效果都进行注册//public static final PotionDeadly DEADLY = new PotionDeadly(true, 0x333333, "deadly", 0);public static final PotionZenHeart ZEN_HEART = new PotionZenHeart(false, 0xcccc00, "zen_heart", 1);//public static final PotionInsidiousDisease BURN = new PotionInsidiousDisease(true, 0x000033, "burn", 2);//public static final PotionErosion EROSION = new PotionErosion(true, 0x660033, "erosion", 8);@Nullableprivate static Potion getRegisteredMobEffect(String id){Potion potion = Potion.REGISTRY.getObject(new ResourceLocation(id));if (potion == null){throw new IllegalStateException("Invalid MobEffect requested: " + id);}else{return potion;}}@SubscribeEventpublic static void registerPotions(RegistryEvent.Register<Potion> event){
// VIRUS_ONE.tuples.add(new EffectTuple(0.2f, MobEffects.NAUSEA, 100));event.getRegistry().registerAll(INSTANCES.toArray(new Potion[0]));IdlFramework.LogWarning("registered %d potion", INSTANCES.size());}@SubscribeEventpublic static void registerPotionTypes(RegistryEvent.Register<PotionType> evt) {InitPotionTypes.init();evt.getRegistry().registerAll(TYPE_INSTANCES.toArray(new PotionType[0]));IdlFramework.Log("registered %d potion item(s)", TYPE_INSTANCES.size());}}
4.在potion包中新建ModPotionType类,用于说明我们的药水类型:
ModPotionType.java
package com.joy187.rejoymod.potion;import com.joy187.rejoymod.util.Reference;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionType;
import javax.annotation.Nullable;public class ModPotionType extends PotionType {protected ModPotionType(PotionEffect... p_i46739_1_) {this(null, p_i46739_1_);}public ModPotionType(@Nullable String p_i46740_1_, PotionEffect... p_i46740_2_) {super(p_i46740_1_, p_i46740_2_);setRegistryName(Reference.Mod_ID, p_i46740_1_);ModPotions.TYPE_INSTANCES.add(this);}
}
5.在init包中新建InitPotionTypes类,用于对我们的药水效果类型进行初始化工作:
InitPotionTypes.java
package com.joy187.rejoymod.init;
import com.joy187.rejoymod.item.ModItems;
import com.joy187.rejoymod.potion.ModPotionType;
import com.joy187.rejoymod.potion.ModPotions;
import net.minecraft.init.*;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionHelper;
import net.minecraft.potion.PotionType;import static net.minecraft.init.PotionTypes.AWKWARD;public class InitPotionTypes {public static final PotionType BLINDNESS;public static final PotionType LUCKY;public static final PotionType UNLUCKY;public static final PotionType HASTE;public static final PotionType RESISTANCE;public static final PotionType WITHER;public static final PotionType LEVITATION;public static final PotionType GLOWING;//public static final PotionType LONG_BLINDNESS;public static final int DURA_GOOD_3 = 1600;public static final int DURA_GOOD_4 = 1200;public static final ModPotionType REGEN_3 = new ModPotionType("regen_3", new PotionEffect(MobEffects.REGENERATION, 450, 2));public static final ModPotionType REGEN_4 = new ModPotionType("regen_4", new PotionEffect(MobEffects.REGENERATION, 200, 3));static{if (!Bootstrap.isRegistered()){throw new RuntimeException("Accessed Potions before Bootstrap!");}else{BLINDNESS = createType(MobEffects.BLINDNESS);LUCKY = createType(MobEffects.LUCK);UNLUCKY = createType(MobEffects.UNLUCK);HASTE = createType(MobEffects.HASTE);RESISTANCE = createType(MobEffects.RESISTANCE);WITHER = createType(MobEffects.WITHER);LEVITATION = createType(MobEffects.LEVITATION);GLOWING = createType(MobEffects.GLOWING);}}public static void init(){//Modded potion effectsPotionHelper.addMix(AWKWARD, ModItems.HUMUS, ModPotions.DEADLY.getPotionType());PotionHelper.addMix(AWKWARD, ModItems.HUMUS, ModPotions.ZEN_HEART.getPotionType());// PotionHelper.addMix(AWKWARD, ModItems.CHILLI, ModPotions.VIRUS_ONE.getPotionType());
// PotionHelper.addMix(AWKWARD, Items.ROTTEN_FLESH, ModPotions.UNDYING.getPotionType());//-----------------------------Types}static ModPotionType createType(Potion potion){//mostly for vanillareturn new ModPotionType(potion.getName(), getEffect(potion));}public static PotionEffect getEffect(Potion potion){return new PotionEffect(potion, potion.isBadEffect() ? 1800 : 3600);}
}
6.在resources包中的lang文件中添加我们所有药水效果的名称
en_us.lang
中添加:
#Potions
rejoymod.potion.zen_heart=Virus Power
#rejoymod.potion.burn=E Decay
#rejoymod.potion.erosion=Erosion Biohazard
在textures\misc中添加我们potions贴图,名称为potions.png(第一步中的ResourceLocation)
我们的potions图片(可直接下载),从第二行最左边开始第一个icon(0号)开始,往右依次加1作为其序号:
7.保存项目 -> 启动游戏调试
我们可以用指令来给予自己的buff效果:
/effect @p modid:buff名称
以/effect @p rejoymod:zen_heart
为例:
可以看到我们的buff效果的确出现了,同时我们的生命回复效果在buff期间一直非常强。
Minecraft 1.12.2模组开发(四十) buff效果(Potion Effect)相关推荐
- Minecraft 1.12.2模组开发(四十九) 维度空间
1.18.2维度空间教程 1.16.5维度空间教程 我们今天在1.12.2的模组中实现一个自定义维度的生成: 1.在Java包的init包中新建一个InitDimension类注册我们所有的维度: I ...
- Minecraft 1.12.2模组开发(四十五) 水火两用船
今天我们在MC中实现一艘可以在水中和岩浆中滑行的船 本期教程之前可以先复习一下生物实体教程,会比较好理解一些. 1.船是一种生物实体,所以我们首先要制作船的模型,这里直接使用了原版的船模型,当然你也可 ...
- Minecraft 1.12.2模组开发(二十) 导出模组
模组制作完成后,我们就可以将其导出成为.jar文件,进行发布,供全世界的玩家进行下载游玩了 1.进入模组开发的文件夹 -> 起动cmd控制台 2.输入构建指令等待其构建模组 Windows系统: ...
- Minecraft 1.16.5模组开发(四十八) 传送门
Minecraft 1.18.2模组 传送门教程 我们今天在模组中实现一个传送门,让我们可以传送到自己的维度中.(文末附数据包下载链接) 1.在src\main\resources\data中新建一个 ...
- Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
今天我们在模组中实现一个自定义盾牌 1.新建一个接口类IHasModel: IHasModel.java public interface IHasModel {public void registe ...
- Minecraft 1.12.2模组开发(三十九) 反应器(TileEntity方块实体)
说到方块实体(TileEntity),可以理解为一种功能性方块,比如熔炉,箱子,附魔台等. 我们今天来做一个类似于熔炉的反应器 熔炉逻辑: 放入燃料-> 放入物品 -> 获取产出物品 1. ...
- Minecraft 1.16.5模组开发(三十八) 3D盔甲(新)
Minecraft升级到1.16.5后,3D盔甲的制作方法也跟之前版本稍有不同(主要在第二步.第四步),建议先复习一下往期教程: Minecraft 1.12.2模组开发(三十七) 3D盔甲 1.在b ...
- Minecraft 1.16.5模组开发(三十二) 自定义投掷物品实体
如果你了解过之前我们的实体开发教程,那么本次的教程会相对比较好理解. Minecraft 1.12.2模组开发(七) 实体(魔改Zombie) 我们本次将参考雪球在MC中制作一个属于我们自己的可投掷实 ...
- Minecraft 1.12.2模组开发(一) 配置ForgeMDK环境
我的世界1.12.2 IDEA开发包构建教程已出,更加方便快捷~ 1.12.2Forge模组开发 配置ForgeMDK环境 开发环境:eclipse 1. 我的世界模组开发首先需要配置Forge MD ...
最新文章
- C#中ref和out的原理
- ThinkPHP3.2 G函数代码及 使用方法
- python变量类型是集合_python基础-基本数据类型:集合
- 华为手机充满有提醒吗_2020手机充电速度排名:最快21分钟充满,华为第15名
- 推荐系统组队学习——GBDT+LR
- 浏览器同源策略及跨域的解决方法
- java计算两个字符串格式的时间间隔多少天多少小时多少分钟
- Model Representation--machine learning
- Java常用工具类总结
- EOS开发DApp 创建EOS钱包和账号
- GNSS_NMEA 0183协议的校验和计算方法_C/C++
- 通过在群晖上安装虚拟机,实现群晖与115网盘的双向同步
- 毕业论文文献综述写作技巧,超级详细!
- pandas(综合测试)
- 手机java时代浏览器_巅峰之战 三款最热java手机浏览器横评
- html制作学生信息表静态网页,实验一静态网页制作报告.doc
- 五问补盲(三) | 补盲激光雷达,敢不敢直面新的安全威胁?
- [bzoj] 1597 土地购买 || 斜率优化dp
- 大数据分析课程(数据分析课设--包含代码)
- RBAC数据库的E-R模型