Skip to content

Commit

Permalink
More configurable Magnet (#57)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <dream-master@gmx.net>
  • Loading branch information
Kogepan229 and Dream-Master authored Jan 13, 2025
1 parent 2835b7a commit 46e5d8a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brandon3055.draconicevolution.common.items.tools;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.renderer.texture.IIconRegister;
Expand All @@ -11,6 +12,7 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
Expand All @@ -25,6 +27,8 @@
import com.brandon3055.draconicevolution.common.handler.ConfigHandler;
import com.brandon3055.draconicevolution.common.items.ItemDE;
import com.brandon3055.draconicevolution.common.lib.References;
import com.brandon3055.draconicevolution.common.utills.IConfigurableItem;
import com.brandon3055.draconicevolution.common.utills.ItemConfigField;

import baubles.api.BaubleType;
import baubles.api.IBauble;
Expand All @@ -37,7 +41,7 @@
*/

@Optional.Interface(iface = "baubles.api.IBauble", modid = "Baubles")
public class Magnet extends ItemDE implements IBauble {
public class Magnet extends ItemDE implements IBauble, IConfigurableItem {

private IIcon draconium;
private IIcon awakened;
Expand Down Expand Up @@ -85,9 +89,14 @@ public boolean hasEffect(ItemStack stack, int pass) {

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean hotbar) {
if (!entity.isSneaking() && entity.ticksExisted % 5 == 0
&& isEnabled(stack)
&& entity instanceof EntityPlayer) {
if (entity.ticksExisted % 5 != 0 || !isEnabled(stack)) {
return;
}
if (IConfigurableItem.ProfileHelper.getBoolean(stack, References.MAGNET_SNEAK, true) && entity.isSneaking()) {
return;
}

if (entity instanceof EntityPlayer player) {
int range = stack.getItemDamage() == 0 ? 8 : 32;

List<EntityItem> items = world.getEntitiesWithinAABB(
Expand Down Expand Up @@ -145,7 +154,6 @@ && isEnabled(stack)
entity.posX,
entity.posY,
entity.posZ).expand(4, 4, 4));
EntityPlayer player = (EntityPlayer) entity;
for (EntityXPOrb orb : xp) {
if (orb.field_70532_c == 0 && orb.isEntityAlive()) {
if (MinecraftForge.EVENT_BUS.post(new PlayerPickupXpEvent(player, orb))) continue;
Expand All @@ -164,15 +172,24 @@ && isEnabled(stack)
}

public static boolean isEnabled(ItemStack itemStack) {
return ItemNBTHelper.getBoolean(itemStack, "MagnetEnabled", false);
// For backward compatibility
if (ItemNBTHelper.verifyExistance(itemStack, "MagnetEnabled")) {
final NBTTagCompound nbt = itemStack.getTagCompound();
final boolean enabled = nbt.getBoolean("MagnetEnabled");
IConfigurableItem.ProfileHelper.setBoolean(itemStack, References.ENABLED, enabled);
nbt.removeTag("MagnetEnabled");
return enabled;
}
return IConfigurableItem.ProfileHelper.getBoolean(itemStack, References.ENABLED, false);
}

public static void toggle(ItemStack itemStack) {
ItemNBTHelper.setBoolean(itemStack, "MagnetEnabled", !isEnabled(itemStack));
final boolean enabled = IConfigurableItem.ProfileHelper.getBoolean(itemStack, References.ENABLED, false);
IConfigurableItem.ProfileHelper.setBoolean(itemStack, References.ENABLED, !enabled);
}

public static void setStatus(ItemStack itemStack, boolean status) {
ItemNBTHelper.setBoolean(itemStack, "MagnetEnabled", status);
IConfigurableItem.ProfileHelper.setBoolean(itemStack, References.ENABLED, status);
}

@Override
Expand Down Expand Up @@ -230,4 +247,17 @@ public boolean canEquip(ItemStack itemstack, EntityLivingBase player) {
public boolean canUnequip(ItemStack itemstack, EntityLivingBase player) {
return true;
}

@Override
public List<ItemConfigField> getFields(ItemStack stack, int slot) {
List<ItemConfigField> fields = new ArrayList<>();
fields.add(new ItemConfigField(References.BOOLEAN_ID, slot, References.ENABLED).readFromItem(stack, false));
fields.add(new ItemConfigField(References.BOOLEAN_ID, slot, References.MAGNET_SNEAK).readFromItem(stack, true));
return fields;
}

@Override
public boolean hasProfiles() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public final class References {
public static final String OBLITERATE = "ToolVoidJunk";
public static final String TREE_MODE = "AxeTreeMode";
public static final String BASE_SAFE_AOE = "BaseSafeAOE";
public static final String ENABLED = "Enabled";
public static final String MAGNET_SNEAK = "MagnetSneak";
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/draconicevolution/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ button.de.BowExplosionPower.name=Explosive Power
button.de.BowShockWavePower.name=Shock Wave Power
button.de.BowEnergyBolt.name=Arrow of Draconic Fire
button.de.BowZoomModifier.name=Magnification
button.de.Enabled.name=Enabled
button.de.MagnetSneak.name=Disabled while sneaking

button.de.reactorCharge.txt=Charge Reactor
button.de.reactorStart.txt=Activate
Expand Down

0 comments on commit 46e5d8a

Please sign in to comment.