Skip to content

Instantly share code, notes, and snippets.

@keepoff07
Last active August 29, 2015 14:19
Show Gist options
  • Save keepoff07/5ce541688e082f3c7c10 to your computer and use it in GitHub Desktop.
Save keepoff07/5ce541688e082f3c7c10 to your computer and use it in GitHub Desktop.
ポーション

なにこれ

この前のやつ使ってダメージ値からPotionEffectを返すものを作った。

ポーションは初期状態で細かいデータタグを何も持っていないため、
PotionMetagetCustomEffects()から取得するが出来ません。
だからこんな面倒なもの書いてみました。

説明

PotionEffectの三次元配列用意して色々。
第一位配列のサイズは2、ここで飲むポーション(0)か、スプラッシュ(1)か。
第二位配列のサイズは16、前のやつに「0~15で種類が決まる」的なのを利用してここで種類分け。
第三位配列のサイズは4、それぞれ初期状態(0)、効力増加(1)、延長(2)、効力増加+延長(3)。

getDefault(short)では、
まず与えられたダメージ値を、スプラッシュポーションの堺である16384で割った商(0か1)をし、
ダメージ値を、大まかな区切りである128で割った余りを求める(minID)。
このminIDを16で割った余りでポーションの種類(0~15)が分かり、
同じくminIDを32で割った商で発展状態(0~3)が分かる。
これを先の配列に入れておしまい。

ちなみに、1.7だと1.8からの跳躍力上昇のポーションが返ってくることがあるかもしれないから注意。

余談

個人的にこうやって全通り書くのはどうかと思っていて、
最初は、それぞれのポーション(13種)の初期値だけ用意しておいて、
効力Ⅱなら初期値からDuration(継続時間)を1/2して返す…とか考えていたけど、
PotionEffectクラスにsetDuration(int)とか無いから、いちいち新しく生成する必要があった。
そして「打ち消し済み」があったことで、効力Ⅱにならない例外ポーションとかもあって、
結果として全通り書いたほうが処理的には速かった。

CraftBukkitの方のCraftPotionBrewerクラスに、(Collection<PotionEffect>)getEffectsFromDamage(int)って
明らかなメソッドがあるんだけど、なんかリフレクション上手く行かなかったからやめた。
(これやるくらいならいちいち計算させる)

import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class DefaultPotion {
private static PotionEffect[][][] effect = {
{
{
null,
null,
null,
null
},
{
new PotionEffect(PotionEffectType.REGENERATION, 900, 0),
new PotionEffect(PotionEffectType.REGENERATION, 450, 1),
new PotionEffect(PotionEffectType.REGENERATION, 2400, 0),
new PotionEffect(PotionEffectType.REGENERATION, 1200, 1)
},
{
new PotionEffect(PotionEffectType.SPEED, 3600, 0),
new PotionEffect(PotionEffectType.SPEED, 1800, 1),
new PotionEffect(PotionEffectType.SPEED, 9600, 0),
new PotionEffect(PotionEffectType.SPEED, 4800, 1)
},
{
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 3600, 0),
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 3600, 0),
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 9600, 0),
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 9600, 0)
},
{
new PotionEffect(PotionEffectType.POISON, 900, 0),
new PotionEffect(PotionEffectType.POISON, 450, 1),
new PotionEffect(PotionEffectType.POISON, 2400, 0),
new PotionEffect(PotionEffectType.POISON, 1200, 1)
},
{
new PotionEffect(PotionEffectType.HEAL, 1, 0),
new PotionEffect(PotionEffectType.HEAL, 1, 1),
new PotionEffect(PotionEffectType.HEAL, 1, 0),
new PotionEffect(PotionEffectType.HEAL, 1, 1)
},
{
new PotionEffect(PotionEffectType.NIGHT_VISION, 3600, 0),
new PotionEffect(PotionEffectType.NIGHT_VISION, 3600, 0),
new PotionEffect(PotionEffectType.NIGHT_VISION, 9600, 0),
new PotionEffect(PotionEffectType.NIGHT_VISION, 9600, 0)
},
{
null,
null,
null,
null
},
{
new PotionEffect(PotionEffectType.WEAKNESS, 1800, 0),
new PotionEffect(PotionEffectType.WEAKNESS, 1800, 0),
new PotionEffect(PotionEffectType.WEAKNESS, 4800, 0),
new PotionEffect(PotionEffectType.WEAKNESS, 4800, 0)
},
{
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 3600, 0),
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1800, 1),
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 9600, 0),
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 4800, 1)
},
{
new PotionEffect(PotionEffectType.SLOW, 1800, 0),
new PotionEffect(PotionEffectType.SLOW, 1800, 0),
new PotionEffect(PotionEffectType.SLOW, 4800, 0),
new PotionEffect(PotionEffectType.SLOW, 4800, 0)
},
{
new PotionEffect(PotionEffectType.JUMP, 3600, 0),
new PotionEffect(PotionEffectType.JUMP, 1800, 1),
new PotionEffect(PotionEffectType.JUMP, 9600, 0),
new PotionEffect(PotionEffectType.JUMP, 4800, 1)
},
{
new PotionEffect(PotionEffectType.HARM, 1, 0),
new PotionEffect(PotionEffectType.HARM, 1, 1),
new PotionEffect(PotionEffectType.HARM, 1, 0),
new PotionEffect(PotionEffectType.HARM, 1, 1)
},
{
new PotionEffect(PotionEffectType.WATER_BREATHING, 3600, 0),
new PotionEffect(PotionEffectType.WATER_BREATHING, 3600, 0),
new PotionEffect(PotionEffectType.WATER_BREATHING, 9600, 0),
new PotionEffect(PotionEffectType.WATER_BREATHING, 9600, 0)
},
{
new PotionEffect(PotionEffectType.INVISIBILITY, 3600, 0),
new PotionEffect(PotionEffectType.INVISIBILITY, 3600, 0),
new PotionEffect(PotionEffectType.INVISIBILITY, 9600, 0),
new PotionEffect(PotionEffectType.INVISIBILITY, 9600, 0)
},
{
null,
null,
null,
null
}
},
{
{
null,
null,
null,
null
},
{
new PotionEffect(PotionEffectType.REGENERATION, 675, 0),
new PotionEffect(PotionEffectType.REGENERATION, 338, 1),
new PotionEffect(PotionEffectType.REGENERATION, 1800, 0),
new PotionEffect(PotionEffectType.REGENERATION, 900, 1)
},
{
new PotionEffect(PotionEffectType.SPEED, 2700, 0),
new PotionEffect(PotionEffectType.SPEED, 1350, 1),
new PotionEffect(PotionEffectType.SPEED, 7200, 0),
new PotionEffect(PotionEffectType.SPEED, 3600, 1)
},
{
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 2700, 0),
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 2700, 0),
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 7200, 0),
new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 7200, 0)
},
{
new PotionEffect(PotionEffectType.POISON, 675, 0),
new PotionEffect(PotionEffectType.POISON, 338, 1),
new PotionEffect(PotionEffectType.POISON, 1800, 0),
new PotionEffect(PotionEffectType.POISON, 900, 1)
},
{
new PotionEffect(PotionEffectType.HEAL, 1, 0),
new PotionEffect(PotionEffectType.HEAL, 1, 1),
new PotionEffect(PotionEffectType.HEAL, 1, 0),
new PotionEffect(PotionEffectType.HEAL, 1, 1)
},
{
new PotionEffect(PotionEffectType.NIGHT_VISION, 2700, 0),
new PotionEffect(PotionEffectType.NIGHT_VISION, 2700, 0),
new PotionEffect(PotionEffectType.NIGHT_VISION, 7200, 0),
new PotionEffect(PotionEffectType.NIGHT_VISION, 7200, 0)
},
{
null,
null,
null,
null
},
{
new PotionEffect(PotionEffectType.WEAKNESS, 1350, 0),
new PotionEffect(PotionEffectType.WEAKNESS, 1350, 0),
new PotionEffect(PotionEffectType.WEAKNESS, 3600, 0),
new PotionEffect(PotionEffectType.WEAKNESS, 3600, 0)
},
{
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2700, 0),
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1350, 1),
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 7200, 0),
new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 3600, 1)
},
{
new PotionEffect(PotionEffectType.SLOW, 1350, 0),
new PotionEffect(PotionEffectType.SLOW, 1350, 0),
new PotionEffect(PotionEffectType.SLOW, 3600, 0),
new PotionEffect(PotionEffectType.SLOW, 3600, 0)
},
{
new PotionEffect(PotionEffectType.JUMP, 2700, 0),
new PotionEffect(PotionEffectType.JUMP, 1350, 1),
new PotionEffect(PotionEffectType.JUMP, 7200, 0),
new PotionEffect(PotionEffectType.JUMP, 3600, 1)
},
{
new PotionEffect(PotionEffectType.HARM, 1, 0),
new PotionEffect(PotionEffectType.HARM, 1, 1),
new PotionEffect(PotionEffectType.HARM, 1, 0),
new PotionEffect(PotionEffectType.HARM, 1, 1)
},
{
new PotionEffect(PotionEffectType.WATER_BREATHING, 2700, 0),
new PotionEffect(PotionEffectType.WATER_BREATHING, 2700, 0),
new PotionEffect(PotionEffectType.WATER_BREATHING, 7200, 0),
new PotionEffect(PotionEffectType.WATER_BREATHING, 7200, 0)
},
{
new PotionEffect(PotionEffectType.INVISIBILITY, 2700, 0),
new PotionEffect(PotionEffectType.INVISIBILITY, 2700, 0),
new PotionEffect(PotionEffectType.INVISIBILITY, 7200, 0),
new PotionEffect(PotionEffectType.INVISIBILITY, 7200, 0)
},
{
null,
null,
null,
null
}
}
};
public static PotionEffect getDefault(short damage) {
int isSplash = damage / 16384;
int minID = damage % 128;
int mainID = minID % 16;
int subID = minID / 32;
return effect[isSplash][mainID][subID];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment