Skip to content

Commit

Permalink
Refactoring, #40
Browse files Browse the repository at this point in the history
  • Loading branch information
NightKosh committed Nov 26, 2018
1 parent 302ea4d commit e50c615
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minecraft.block.material.Material;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.AbstractSkeleton;
Expand Down Expand Up @@ -44,9 +43,8 @@
import nightkosh.gravestone_extended.entity.monster.crawler.EntityWitherSkullCrawler;
import nightkosh.gravestone_extended.entity.monster.crawler.EntityZombieSkullCrawler;
import nightkosh.gravestone_extended.helper.CemeteryHelper;
import nightkosh.gravestone_extended.item.weapon.IBoneShiled;
import nightkosh.gravestone_extended.helper.GSEnchantmentHelper;
import nightkosh.gravestone_extended.item.weapon.IBoneSword;
import nightkosh.gravestone_extended.item.weapon.ItemBoneShield;
import nightkosh.gravestone_extended.potion.PotionBleeding;
import nightkosh.gravestone_extended.potion.PotionPurification;
import org.lwjgl.opengl.GL11;
Expand Down Expand Up @@ -109,16 +107,7 @@ public void livingDamageEvent(LivingDamageEvent event) {
@SubscribeEvent
public void livingAttackEvent(LivingAttackEvent event) {
if (event.getEntityLiving() instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) event.getEntityLiving();
ItemStack stack = player.getActiveItemStack();
if (!stack.isEmpty() && stack.getItem() instanceof IBoneShiled) {
float amount = event.getAmount();

EnchantmentPainMirror.applyEnchantmentEffect(player, event.getSource().getTrueSource(), stack, amount);

if (event.getAmount() >= 3)
((ItemBoneShield) stack.getItem()).damageShield(stack, player, amount);
}
EnchantmentPainMirror.applyEffect((EntityPlayer) event.getEntityLiving(), event.getSource().getTrueSource(), event.getAmount());
}
}

Expand Down Expand Up @@ -146,7 +135,7 @@ public void livingHealEvent(LivingHealEvent event) {
// if (!event.getItem().isEmpty()) {
// NBTTagList nbtList = event.getItem().getEnchantmentTagList();
// for (NBTBase nbt : nbtList) {
// if (((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(GSEnchantment.CURSE_FRAGILITY)) {
// if (GSEnchantmentHelper.hasEnchantment(nbt, GSEnchantment.CURSE_FRAGILITY)) {
// event.getItem().damageItem(2, event.getEntityLiving());
// }
// }
Expand All @@ -155,19 +144,7 @@ public void livingHealEvent(LivingHealEvent event) {

@SubscribeEvent(priority = EventPriority.HIGHEST)
public void itemFishedEvent(ItemFishedEvent event) {
ItemStack stack = event.getEntityPlayer().getHeldItemMainhand();

if (!stack.isEmpty()) {
NBTTagList nbtList = stack.getEnchantmentTagList();
for (NBTBase nbt : nbtList) {
if (((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(GSEnchantment.CURSE_BROKEN_HOOK)) {
if (EnchantmentBrokenHookCurse.cancelFishing()) {
event.setCanceled(true);
}
break;
}
}
}
EnchantmentBrokenHookCurse.applyEffect(event);
}

@SubscribeEvent(priority = EventPriority.HIGH)
Expand All @@ -190,9 +167,9 @@ public void onEntityLivingDamage(LivingDamageEvent event) {
private static void applyEntityLivingDamageEnchantments(EntityLivingBase attacker, EntityLivingBase target, ItemStack weapon, float damage) {
NBTTagList nbtList = weapon.getEnchantmentTagList();
for (NBTBase nbt : nbtList) {
if (((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(GSEnchantment.VAMPIRIC_TOUCH)) {
if (GSEnchantmentHelper.hasEnchantment(nbt, GSEnchantment.VAMPIRIC_TOUCH)) {
EnchantmentVampiricTouch.applyEnchantmentEffect(attacker, damage);
} else if (((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(GSEnchantment.NECROTIC_CORROSION)) {
} else if (GSEnchantmentHelper.hasEnchantment(nbt, GSEnchantment.NECROTIC_CORROSION)) {
EnchantmentNecroticCorrosion.applyEnchantmentEffect(target, damage, ((NBTTagCompound) nbt).getShort("lvl"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class EnchantmentBase extends Enchantment {
EntityEquipmentSlot.OFFHAND
};

protected EnchantmentBase(Rarity rarityIn, EnumEnchantmentType typeIn, EntityEquipmentSlot[] slots) {
super(rarityIn, typeIn, slots);
protected EnchantmentBase(Rarity rarity, EnumEnchantmentType type, EntityEquipmentSlot[] slots) {
super(rarity, type, slots);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockLog;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.BlockPos;
import nightkosh.gravestone_extended.core.GSEnchantment;
import nightkosh.gravestone_extended.core.ModInfo;
import nightkosh.gravestone_extended.helper.GSEnchantmentHelper;
import nightkosh.gravestone_extended.item.tools.axe.IBoneAxe;

/**
Expand All @@ -23,7 +20,7 @@
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentBloodyReplication extends EnchantmentBase {
public class EnchantmentBloodyReplication extends EnchantmentTreasure {

public EnchantmentBloodyReplication() {
super(Rarity.VERY_RARE, EnumEnchantmentType.DIGGER, new EntityEquipmentSlot[]{EntityEquipmentSlot.MAINHAND});
Expand Down Expand Up @@ -56,34 +53,31 @@ public static boolean applyEffect(EntityLivingBase entity, IBlockState state, Bl
ItemStack axe = entity.getHeldItemMainhand();
if (axe.getItem() instanceof IBoneAxe) {
if (!axe.isEmpty()) {
NBTTagList nbtList = axe.getEnchantmentTagList();
for (NBTBase nbt : nbtList) {
if (((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(GSEnchantment.BLOODY_REPLICATION)) {
Block block = state.getBlock();
int level = ((NBTTagCompound) nbt).getInteger("lvl");
if (!(level == 4 && entity.getHealth() < 0.3) && block instanceof BlockLog) {
block.dropBlockAsItem(entity.world, entity.getPosition(), state, 0);
float damage;
switch (level) {
case 1:
damage = 1.5F;
break;
case 2:
damage = 1;
break;
case 3:
damage = 0.6F;
break;
default:
damage = 0.25F;
break;
}
entity.attackEntityFrom(DamageSource.MAGIC, damage);
axe.damageItem(1, entity);
return true;
} else {
return false;
int level = GSEnchantmentHelper.getEnchantmentLevel(axe, GSEnchantment.BLOODY_REPLICATION);
if (level > 0) {
Block block = state.getBlock();
if (!(level == 4 && entity.getHealth() < 0.3) && block instanceof BlockLog) {
block.dropBlockAsItem(entity.world, entity.getPosition(), state, 0);
float damage;
switch (level) {
case 1:
damage = 1.5F;
break;
case 2:
damage = 1;
break;
case 3:
damage = 0.6F;
break;
default:
damage = 0.25F;
break;
}
entity.attackEntityFrom(DamageSource.MAGIC, damage);
axe.damageItem(1, entity);
return true;
} else {
return false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentBoneRain extends EnchantmentBase {
public class EnchantmentBoneRain extends EnchantmentTreasure {
public EnchantmentBoneRain() {
super(Rarity.VERY_RARE, EnumEnchantmentType.ALL, new EntityEquipmentSlot[]{EntityEquipmentSlot.MAINHAND});
this.setName("bone_rain");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentFrozenNether extends EnchantmentBase {
public class EnchantmentFrozenNether extends EnchantmentTreasure {

public EnchantmentFrozenNether() {
super(Rarity.VERY_RARE, EnumEnchantmentType.ARMOR_FEET, new EntityEquipmentSlot[]{EntityEquipmentSlot.FEET});
Expand Down Expand Up @@ -56,7 +56,7 @@ public boolean canApply(ItemStack stack) {
public static void applyEffect(EntityLivingBase entity) {
World world = entity.getEntityWorld();
if (!world.isRemote) {
int level = GSEnchantmentHelper.getMaxEnchantmentLevel(entity.getItemStackFromSlot(EntityEquipmentSlot.FEET), GSEnchantment.FROZEN_NETHER);
int level = GSEnchantmentHelper.getEnchantmentLevel(entity.getItemStackFromSlot(EntityEquipmentSlot.FEET), GSEnchantment.FROZEN_NETHER);

if (level > 0) {
BlockPos blockPos = entity.getPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentHellishAngling extends EnchantmentBase {
public class EnchantmentHellishAngling extends EnchantmentTreasure {

public EnchantmentHellishAngling() {
super(Rarity.VERY_RARE, EnumEnchantmentType.FISHING_ROD, new EntityEquipmentSlot[]{EntityEquipmentSlot.MAINHAND});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentNecroticCorrosion extends EnchantmentBase {
public class EnchantmentNecroticCorrosion extends EnchantmentTreasure {

public EnchantmentNecroticCorrosion() {
super(Rarity.VERY_RARE, EnumEnchantmentType.WEAPON, new EntityEquipmentSlot[] {EntityEquipmentSlot.MAINHAND});
Expand All @@ -40,6 +40,7 @@ public int getMaxLevel() {
public boolean canApply(ItemStack stack) {
return super.canApply(stack) && stack.getItem() instanceof IBoneSword;
}

public static void applyEnchantmentEffect(EntityLivingBase target, float damage, short level) {
float additionalDamage = damage * 0.1F * level;
if (additionalDamage < 0.5) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package nightkosh.gravestone_extended.enchantment;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBook;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.DamageSource;
import nightkosh.gravestone_extended.core.GSEnchantment;
import nightkosh.gravestone_extended.core.ModInfo;
import nightkosh.gravestone_extended.helper.GSEnchantmentHelper;
import nightkosh.gravestone_extended.item.weapon.IBoneShiled;
import nightkosh.gravestone_extended.item.weapon.ItemBoneShield;

/**
* GraveStone mod
*
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentPainMirror extends EnchantmentBase {
public class EnchantmentPainMirror extends EnchantmentTreasure {

public EnchantmentPainMirror() {
super(Rarity.VERY_RARE, EnumEnchantmentType.ALL, HAND_SLOTS);
Expand Down Expand Up @@ -54,16 +52,22 @@ public boolean canApply(ItemStack stack) {
return stack.getItem() instanceof IBoneShiled || stack.getItem() instanceof ItemBook;
}

public static void applyEnchantmentEffect(EntityPlayer player, Entity attacker, ItemStack stack, float amount) {
NBTTagList nbtList = stack.getEnchantmentTagList();
for (NBTBase nbt : nbtList) {
if (((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(GSEnchantment.PAIN_MIRROR)) {
if (player.world.rand.nextInt(100) <= 10 * ((NBTTagCompound) nbt).getShort("lvl")) {
public static void applyEffect(EntityPlayer player, Entity attacker, float amount) {
ItemStack stack = player.getActiveItemStack();
if (!stack.isEmpty() && stack.getItem() instanceof IBoneShiled) {

int level = GSEnchantmentHelper.getEnchantmentLevel(stack, GSEnchantment.PAIN_MIRROR);
if (level > 0) {
if (player.world.rand.nextInt(100) <= 10 * level) {
if (attacker instanceof EntityLivingBase) {
attacker.attackEntityFrom(DamageSource.MAGIC, amount);
}
}
}

if (amount >= 3) {
((ItemBoneShield) stack.getItem()).damageShield(stack, player, amount);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nightkosh.gravestone_extended.enchantment;

import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.inventory.EntityEquipmentSlot;

/**
* GraveStone mod
*
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public abstract class EnchantmentTreasure extends EnchantmentBase {

protected EnchantmentTreasure(Rarity rarity, EnumEnchantmentType type, EntityEquipmentSlot[] slots) {
super(rarity, type, slots);
}

@Override
public boolean isTreasureEnchantment() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @author NightKosh
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class EnchantmentVampiricTouch extends EnchantmentBase {
public class EnchantmentVampiricTouch extends EnchantmentTreasure {

public EnchantmentVampiricTouch() {
super(Enchantment.Rarity.VERY_RARE, EnumEnchantmentType.WEAPON, new EntityEquipmentSlot[] {EntityEquipmentSlot.MAINHAND});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraftforge.event.entity.player.ItemFishedEvent;
import nightkosh.gravestone_extended.core.GSEnchantment;
import nightkosh.gravestone_extended.core.ModInfo;
import nightkosh.gravestone_extended.helper.GSEnchantmentHelper;

import java.util.Random;

Expand All @@ -16,12 +19,15 @@ public class EnchantmentBrokenHookCurse extends EnchantmentCurse {
private static final Random RANDOM = new Random();

public EnchantmentBrokenHookCurse() {
super(EnumEnchantmentType.FISHING_ROD, new EntityEquipmentSlot[] {EntityEquipmentSlot.MAINHAND});
super(EnumEnchantmentType.FISHING_ROD, new EntityEquipmentSlot[]{EntityEquipmentSlot.MAINHAND});
this.setName("broken_hook_curse");
this.setRegistryName(ModInfo.ID, "gs_broken_hook_curse");
}

public static boolean cancelFishing() {
return RANDOM.nextInt(10) < 3;
public static void applyEffect(ItemFishedEvent event) {
int level = GSEnchantmentHelper.getEnchantmentLevel(event.getEntityPlayer().getHeldItemMainhand(), GSEnchantment.CURSE_BROKEN_HOOK);
if (level > 0 && RANDOM.nextInt(10) < 3) {
event.setCanceled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
*/
public class GSEnchantmentHelper extends nightkosh.gravestone.helper.EnchantmentHelper {

public static short getMaxEnchantmentLevel(ItemStack stack, Enchantment enchantment) {
public static boolean hasEnchantment(NBTBase nbt, Enchantment enchantment) {
return ((NBTTagCompound) nbt).getInteger("id") == Enchantment.getEnchantmentID(enchantment);
}

public static short getEnchantmentLevel(ItemStack stack, Enchantment enchantment) {
if (stack != null && stack != ItemStack.EMPTY) {
NBTTagList nbtList = stack.getEnchantmentTagList();
for (NBTBase nbt : nbtList) {
Expand Down

0 comments on commit e50c615

Please sign in to comment.