目前正在尝试创建一种药水效果,一旦时间耗尽,就会对玩家应用其他药水效果。看起来很简单,但我在尝试完成此任务时发现了一些错误和错误,
直接尝试添加效果
@Override
public void performEffect(EntityLivingBase entity, int amplifier){
if (entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer)entity;
if(player != null){
if(player.getActivePotionEffect(PotionRegistry.effectBuzz) != null){
int duraction = player.getActivePotionEffect(PotionRegistry.effectBuzz).getDuration();
if(duration <= 2){
player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 1200));
}
}
}
}
}
不用说这会产生这个错误
[16:10:04] [Server thread/ERROR]: Encountered an unexpected exception net.minecraft.util.ReportedException: Ticking player at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:212) ~[NetworkSystem.class:?] at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:807) ~[MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:688) ~[MinecraftServer.class:?] at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) ~[IntegratedServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537) [MinecraftServer.class:?] at java.lang.Thread.run(Unknown Source) [?:1.8.0_161] Caused by: java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextNode(Unknown Source) ~[?:1.8.0_161] at java.util.HashMap$KeyIterator.next(Unknown Source) ~[?:1.8.0_161] at net.minecraft.entity.EntityLivingBase.updatePotionEffects(EntityLivingBase.java:650) ~[EntityLivingBase.class:?] at net.minecraft.entity.EntityLivingBase.onEntityUpdate(EntityLivingBase.java:383) ~[EntityLivingBase.class:?] at net.minecraft.entity.Entity.onUpdate(Entity.java:436) ~[Entity.class:?] at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2144) ~[EntityLivingBase.class:?] at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:260) ~[EntityPlayer.class:?] at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:345) ~[EntityPlayerMP.class:?] at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174) ~[NetHandlerPlayServer.class:?] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:216) ~[NetworkDispatcher$1.class:?] at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:309) ~[NetworkManager.class:?] at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197) ~[NetworkSystem.class:?] ... 5 more
就好像我在勾选事件中运行它
在 CommonProxy 中
MinecraftForge.EVENT_BUS.register(new EventManager());
然后对于 EventManager 本身
public class EventManager {
public static PotionEffect potion = new PotionEffect(MobEffects.WEAKNESS, 1200);
public static PotionEffect potion2 = new PotionEffect(MobEffects.HUNGER, 600);
public static PotionEffect potion3 = new PotionEffect(MobEffects.UNLUCK, 1200);
@SubscribeEvent
public void onTick(WorldTickEvent event){
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
World world = Minecraft.getMinecraft().theWorld;
if(player != null){
boolean hasEffect = player.isPotionActive(PotionRegistry.effectBuzz);
int applyIt = 0;
if(hasEffect){
applyIt = 1;
} else if(!player.isPotionActive(potion.getPotion()) && applyIt == 1){
applyIt = 2;
} else {
applyIt = 0;
}
if(player != null && applyIt == 2){
player.addPotionEffect(potion);
}
}
}
}
这可行,但效果是无限的。
最佳答案
您正在执行您的操作,同时药水效果正在循环。这类似于在迭代数组时修改数组。不要这样做。
此外,不要在客户端执行类似药水效果的操作。 客户端要做的唯一事情是图形和用户输入/输出。
像药水这样的事情必须在服务器上处理,否则服务器会覆盖你在下一个更新包上的操作。
只需在 ExtendPlayer 实体中设置一个标志,然后 onTick 或播放器更新事件检查该标志是否存在,然后添加药水。
@Override
public void performEffect(EntityLivingBase entity, int amplifier){
if (entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer)entity;
if(player != null){
if(player.getActivePotionEffect(PotionRegistry.effectBuzz) != null){
int duraction = player.getActivePotionEffect(PotionRegistry.effectBuzz).getDuration();
if(duration <= 2){
ExtendedPlayer ePlayer = ExtendedPlayer.get(player);
ePlayer.enableBuzz();
}
}
}
}
}
类似于扩展播放器的东西
public class ExtendedPlayer implements IExtendedEntityProperties {
... Extended player setup here
protected boolean startBuzz = false;
public void enableBuzz()
{
this.startBuzz = true;
}
public static final ExtendedPlayer get(EntityPlayer player) {
return (ExtendedPlayer) player.getExtendedProperties("MuddymansExtendedPlayer");
}
public EntityPlayer getPlayer() {
return this.player;
}
/**
* Updates anything that needs to be updated each tick
* NOT called automatically, so you must call it yourself from LivingUpdateEvent or a TickHandler
*/
public void onUpdate() {
if(!player.worldObj.isRemote) {
if(this.enableBuzz) {
Player player = this.getPlayer()
player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 1200));
player.addPotionEffect(new PotionEffect(MobEffects.HUNGER, 600));
player.addPotionEffect(new PotionEffect(MobEffects.UNLUCK, 1200));
this.startBuzz = false;
}
}
}
}
从事件处理程序调用扩展的播放器更新事件
@SubscribeEvent
public void livingTick(final LivingUpdateEvent event) {
if (event.entity != null && event.entity instanceof EntityPlayer) {
if(!event.entity.isDead) {
ExtendedPlayer.get((EntityPlayer)event.entity).onUpdate();
}
}
关于java - Minecraft 从自定义药水效果中添加新的药水效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850855/