ItemScroll.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package me.km.items;
  2. import java.util.List;
  3. import me.km.KajetansMod;
  4. import me.km.api.GlobalText;
  5. import me.km.api.Utils;
  6. import me.km.effects.Effect;
  7. import me.km.effects.EffectCause;
  8. import net.minecraft.client.util.ITooltipFlag;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.entity.player.EntityPlayerMP;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.stats.StatList;
  14. import net.minecraft.util.ActionResult;
  15. import net.minecraft.util.EnumActionResult;
  16. import net.minecraft.util.EnumHand;
  17. import net.minecraft.world.World;
  18. import net.minecraftforge.common.capabilities.ICapabilityProvider;
  19. import net.minecraftforge.fml.relauncher.Side;
  20. import net.minecraftforge.fml.relauncher.SideOnly;
  21. public class ItemScroll extends ItemBase
  22. {
  23. public ItemScroll(String name, String local)
  24. {
  25. super(name, local);
  26. }
  27. @Override
  28. public ActionResult<ItemStack> onItemRightClick(World w, EntityPlayer p, EnumHand hand)
  29. {
  30. if(w.isRemote || KajetansMod.singlePlayer)
  31. {
  32. return new ActionResult(EnumActionResult.SUCCESS, p.getHeldItem(hand));
  33. }
  34. if(!KajetansMod.worldManager.getWorldPreferences(w).scrolls)
  35. {
  36. KajetansMod.scrolls.send(p, GlobalText.noScrolls());
  37. return new ActionResult(EnumActionResult.FAIL, p.getHeldItem(hand));
  38. }
  39. ItemStack stack = p.getHeldItem(hand);
  40. NBTTagCompound com = getTagCompound(stack);
  41. int id = com.getInteger("effect_id");
  42. if(id == -1)
  43. {
  44. KajetansMod.scrolls.send(p, "Die Schriftrolle hat keinen Effekt.");
  45. return new ActionResult(EnumActionResult.FAIL, stack);
  46. }
  47. Effect eff = Effect.getEffect(id);
  48. if(eff == null)
  49. {
  50. KajetansMod.scrolls.send(p, "Die Schriftrolle ist korrupt.");
  51. return new ActionResult(EnumActionResult.FAIL, stack);
  52. }
  53. if(eff.getEffectInstance().run((EntityPlayerMP) p, com.getInteger("effect_level"), 0, 20, EffectCause.SCROLL))
  54. {
  55. if(!p.isCreative())
  56. {
  57. stack.shrink(1);
  58. }
  59. p.addStat(StatList.getObjectUseStats(this));
  60. return new ActionResult(EnumActionResult.SUCCESS, stack);
  61. }
  62. return new ActionResult(EnumActionResult.FAIL, stack);
  63. }
  64. @SideOnly(Side.CLIENT)
  65. @Override
  66. public void addInformation(ItemStack stack, World w, List<String> lore, ITooltipFlag flag)
  67. {
  68. NBTTagCompound com = getTagCompound(stack);
  69. String s = com.getString("effect_name");
  70. if(!s.isEmpty())
  71. {
  72. lore.set(0, s + " " + Utils.intToRoman(com.getInteger("effect_level")));
  73. }
  74. }
  75. @Override
  76. public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
  77. {
  78. getTagCompound(stack);
  79. return null;
  80. }
  81. private NBTTagCompound getTagCompound(ItemStack stack)
  82. {
  83. NBTTagCompound com = stack.getTagCompound();
  84. if(com == null)
  85. {
  86. com = new NBTTagCompound();
  87. com.setInteger("effect_id", -1);
  88. com.setInteger("effect_level", 1);
  89. com.setString("effect_name", "");
  90. stack.setTagCompound(com);
  91. }
  92. else
  93. {
  94. if(!com.hasKey("effect_id"))
  95. {
  96. com.setInteger("effect_id", -1);
  97. }
  98. if(!com.hasKey("effect_level"))
  99. {
  100. com.setInteger("effect_level", 1);
  101. }
  102. if(!com.hasKey("effect_name"))
  103. {
  104. com.setString("effect_name", "");
  105. }
  106. }
  107. return com;
  108. }
  109. public void setEffect(ItemStack stack, int id, int level, String name)
  110. {
  111. NBTTagCompound com = getTagCompound(stack);
  112. com.setInteger("effect_id", id);
  113. level = Math.max(1, Math.min(20, level));
  114. com.setInteger("effect_level", level);
  115. com.setString("effect_name", name);
  116. }
  117. }