Skip to content

Commit

Permalink
Migrate all Prometheum related methods to PrometheumHandler
Browse files Browse the repository at this point in the history
Signed-off-by: Noaaan <noaaan@hotmail.com>
  • Loading branch information
Noaaan committed Mar 9, 2024
1 parent cd1aa27 commit f7e5be9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 107 deletions.
34 changes: 1 addition & 33 deletions src/main/java/nourl/mythicmetals/MythicMetals.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@
import io.wispforest.owo.itemgroup.gui.ItemGroupButton;
import io.wispforest.owo.registration.reflect.FieldRegistrationHandler;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.item.v1.ModifyItemAttributeModifiersCallback;
import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper;
import net.fabricmc.fabric.api.registry.FuelRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.dispenser.ProjectileDispenserBehavior;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
Expand All @@ -33,24 +28,19 @@
import nourl.mythicmetals.armor.CarmotShield;
import nourl.mythicmetals.armor.MythicArmor;
import nourl.mythicmetals.blocks.BanglumNukeHandler;
import nourl.mythicmetals.blocks.IndevBlocks;
import nourl.mythicmetals.blocks.MythicBlocks;
import nourl.mythicmetals.command.MythicCommands;
import nourl.mythicmetals.config.MythicMetalsConfig;
import nourl.mythicmetals.data.MythicOreKeys;
import nourl.mythicmetals.data.MythicTags;
import nourl.mythicmetals.effects.MythicStatusEffects;
import nourl.mythicmetals.entity.*;
import nourl.mythicmetals.item.MythicItems;
import nourl.mythicmetals.item.tools.MythicTools;
import nourl.mythicmetals.item.tools.PrometheumToolSet;
import nourl.mythicmetals.misc.*;
import nourl.mythicmetals.registry.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.UUID;

public class MythicMetals implements ModInitializer, EntityComponentInitializer {
public static Logger LOGGER = LogManager.getLogger();
public static final String MOD_ID = "mythicmetals";
Expand Down Expand Up @@ -114,7 +104,7 @@ public void onInitialize() {
factories.add(new TradeOffers.SellItemFactory(MythicItems.Templates.AEGIS_SMITHING_TEMPLATE, 48, 1, 2, 30));
});
registerDispenserBehaviour();
registerPrometheumAttributeEvent();
PrometheumHandler.registerPrometheumAttributeEvent();


if (CONFIG.configVersion() < CONFIG_VERSION) {
Expand Down Expand Up @@ -187,26 +177,4 @@ public void registerEntityComponentFactories(EntityComponentFactoryRegistry regi
registry.registerForPlayers(CARMOT_SHIELD, CarmotShield::new, RespawnCopyStrategy.INVENTORY);
}

private void registerPrometheumAttributeEvent() {
ModifyItemAttributeModifiersCallback.EVENT.register((stack, slot, attributeModifiers) -> {
if (stack.isIn(MythicTags.PROMETHEUM_ARMOR) && ((ArmorItem) stack.getItem()).getSlotType().equals(slot)) {
if (EnchantmentHelper.hasBindingCurse(stack)) {
attributeModifiers.put(EntityAttributes.GENERIC_ATTACK_SPEED, new EntityAttributeModifier(
UUID.fromString("d42e82c8-166d-46f1-bc76-df84e91b5531"),
"Bound Prometheum bonus",
0.08,
EntityAttributeModifier.Operation.MULTIPLY_BASE
));
}
if (PrometheumToolSet.isOvergrown(stack)) {
attributeModifiers.put(EntityAttributes.GENERIC_ARMOR_TOUGHNESS, new EntityAttributeModifier(
UUID.fromString("37bb6460-e896-44e2-8e71-29335d5ce709"),
"Prometheum bonus toughness",
EnchantmentHelper.hasBindingCurse(stack) ? 2 : 1,
EntityAttributeModifier.Operation.ADDITION
));
}
}
});
}
}
9 changes: 3 additions & 6 deletions src/main/java/nourl/mythicmetals/MythicMetalsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
import nourl.mythicmetals.data.MythicTags;
import nourl.mythicmetals.entity.MythicEntities;
import nourl.mythicmetals.item.tools.*;
import nourl.mythicmetals.misc.BlockBreaker;
import nourl.mythicmetals.misc.RegistryHelper;
import nourl.mythicmetals.misc.ShieldUsePredicate;
import nourl.mythicmetals.misc.UsefulSingletonForColorUtil;
import nourl.mythicmetals.misc.*;
import nourl.mythicmetals.mixin.WorldRendererInvoker;
import nourl.mythicmetals.registry.RegisterBlockEntityTypes;

Expand Down Expand Up @@ -111,12 +108,12 @@ public void onInitializeClient() {

private void registerPrometheumTooltips() {
ItemTooltipCallback.EVENT.register((stack, context, lines) -> {
if (stack.isIn(MythicTags.PROMETHEUM_TOOLS) && PrometheumToolSet.isOvergrown(stack)) {
if (stack.isIn(MythicTags.PROMETHEUM_TOOLS) && PrometheumHandler.isOvergrown(stack)) {
Ability.addTooltipOnStack(stack, lines, Style.EMPTY.withColor(UsefulSingletonForColorUtil.MetalColors.PROMETHEUM.rgb()), "tooltip.prometheum.overgrown");
}

if (stack.isIn(MythicTags.PROMETHEUM_ARMOR)) {
if (PrometheumToolSet.isOvergrown(stack)) {
if (PrometheumHandler.isOvergrown(stack)) {
Ability.addTooltipOnStack(stack, lines, Style.EMPTY.withColor(UsefulSingletonForColorUtil.MetalColors.PROMETHEUM.rgb()), "tooltip.prometheum.overgrown");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import nourl.mythicmetals.item.tools.PrometheumToolSet;
import nourl.mythicmetals.misc.PrometheumHandler;

public class PrometheumArmorItem extends ArmorItem {
public PrometheumArmorItem(ArmorMaterial material, Type type, Settings settings) {
Expand All @@ -14,7 +14,7 @@ public PrometheumArmorItem(ArmorMaterial material, Type type, Settings settings)

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient()) PrometheumToolSet.tickAutoRepair(stack, world.getRandom());
if (!world.isClient()) PrometheumHandler.tickAutoRepair(stack, world.getRandom());
super.inventoryTick(stack, world, entity, slot, selected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import nourl.mythicmetals.abilities.DrillUpgrades;
import nourl.mythicmetals.blocks.MythicBlocks;
import nourl.mythicmetals.item.MythicItems;
import nourl.mythicmetals.misc.PrometheumHandler;
import nourl.mythicmetals.misc.UsefulSingletonForColorUtil;
import nourl.mythicmetals.registry.RegisterSounds;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -253,7 +254,7 @@ public void inventoryTick(ItemStack stack, World world, Entity entity, int slot,
stack.put(IS_ACTIVE, false);
}
if (hasUpgradeItem(stack, MythicItems.Mats.PROMETHEUM_BOUQUET)) {
PrometheumToolSet.tickAutoRepair(stack, world.getRandom());
PrometheumHandler.tickAutoRepair(stack, world.getRandom());
}
}
super.inventoryTick(stack, world, entity, slot, selected);
Expand Down
76 changes: 11 additions & 65 deletions src/main/java/nourl/mythicmetals/item/tools/PrometheumToolSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.KeyedEndec;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.attribute.*;
import net.minecraft.item.*;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import nourl.mythicmetals.data.MythicTags;
import nourl.mythicmetals.misc.PrometheumHandler;
import java.util.UUID;

public class PrometheumToolSet extends ToolSet {
/**
* Used to track how much durability is repaired
* @deprecated will be replaced by "mm_durability_repaired"
*/
public static final KeyedEndec<Integer> DURABILITY_REPAIRED = new KeyedEndec<>("DurabilityRepaired", Endec.INT, 0);
private static final int OVERGROWN_THRESHOLD = 1200;

public PrometheumToolSet(ToolMaterial material, int[] damage, float[] speed) {
super(material, damage, speed);
Expand Down Expand Up @@ -63,7 +52,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

var modifiers = HashMultimap.create(super.getAttributeModifiers(slot));

if (stack.has(DURABILITY_REPAIRED) && stack.get(DURABILITY_REPAIRED) > OVERGROWN_THRESHOLD) {
if (stack.has(PrometheumHandler.DURABILITY_REPAIRED) && stack.get(PrometheumHandler.DURABILITY_REPAIRED) > PrometheumHandler.OVERGROWN_THRESHOLD) {
modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE,
new EntityAttributeModifier(
UUID.fromString("69def8b1-1baa-401e-a7cb-b27ab9a55558"),
Expand All @@ -78,7 +67,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient()) tickAutoRepair(stack, world.getRandom());
if (!world.isClient()) PrometheumHandler.tickAutoRepair(stack, world.getRandom());
super.inventoryTick(stack, world, entity, slot, selected);
}
}
Expand All @@ -94,7 +83,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

var modifiers = HashMultimap.create(super.getAttributeModifiers(slot));

if (stack.has(DURABILITY_REPAIRED) && stack.get(DURABILITY_REPAIRED) > OVERGROWN_THRESHOLD) {
if (stack.has(PrometheumHandler.DURABILITY_REPAIRED) && stack.get(PrometheumHandler.DURABILITY_REPAIRED) > PrometheumHandler.OVERGROWN_THRESHOLD) {
modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE,
new EntityAttributeModifier(
UUID.fromString("69def8b1-1baa-401e-a7cb-b27ab9a55558"),
Expand All @@ -109,7 +98,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient()) tickAutoRepair(stack, world.getRandom());
if (!world.isClient()) PrometheumHandler.tickAutoRepair(stack, world.getRandom());
super.inventoryTick(stack, world, entity, slot, selected);
}
}
Expand All @@ -125,7 +114,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

var modifiers = HashMultimap.create(super.getAttributeModifiers(slot));

if (stack.has(DURABILITY_REPAIRED) && stack.get(DURABILITY_REPAIRED) > OVERGROWN_THRESHOLD) {
if (stack.has(PrometheumHandler.DURABILITY_REPAIRED) && stack.get(PrometheumHandler.DURABILITY_REPAIRED) > PrometheumHandler.OVERGROWN_THRESHOLD) {
modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE,
new EntityAttributeModifier(
UUID.fromString("69def8b1-1baa-401e-a7cb-b27ab9a55558"),
Expand All @@ -140,7 +129,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient()) tickAutoRepair(stack, world.getRandom());
if (!world.isClient()) PrometheumHandler.tickAutoRepair(stack, world.getRandom());
super.inventoryTick(stack, world, entity, slot, selected);
}
}
Expand All @@ -156,7 +145,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

var modifiers = HashMultimap.create(super.getAttributeModifiers(slot));

if (stack.has(DURABILITY_REPAIRED) && stack.get(DURABILITY_REPAIRED) > OVERGROWN_THRESHOLD) {
if (stack.has(PrometheumHandler.DURABILITY_REPAIRED) && stack.get(PrometheumHandler.DURABILITY_REPAIRED) > PrometheumHandler.OVERGROWN_THRESHOLD) {
modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE,
new EntityAttributeModifier(
UUID.fromString("69def8b1-1baa-401e-a7cb-b27ab9a55558"),
Expand All @@ -171,7 +160,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient()) tickAutoRepair(stack, world.getRandom());
if (!world.isClient()) PrometheumHandler.tickAutoRepair(stack, world.getRandom());
super.inventoryTick(stack, world, entity, slot, selected);
}
}
Expand All @@ -187,7 +176,7 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

var modifiers = HashMultimap.create(super.getAttributeModifiers(slot));

if (stack.has(DURABILITY_REPAIRED) && stack.get(DURABILITY_REPAIRED) > OVERGROWN_THRESHOLD) {
if (stack.has(PrometheumHandler.DURABILITY_REPAIRED) && stack.get(PrometheumHandler.DURABILITY_REPAIRED) > PrometheumHandler.OVERGROWN_THRESHOLD) {
modifiers.put(EntityAttributes.GENERIC_ATTACK_DAMAGE,
new EntityAttributeModifier(
UUID.fromString("69def8b1-1baa-401e-a7cb-b27ab9a55558"),
Expand All @@ -202,52 +191,9 @@ public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient()) tickAutoRepair(stack, world.getRandom());
if (!world.isClient()) PrometheumHandler.tickAutoRepair(stack, world.getRandom());
super.inventoryTick(stack, world, entity, slot, selected);
}
}

public static void incrementRepairCounter(ItemStack stack, int value) {
int counter = stack.get(DURABILITY_REPAIRED);
if (counter < (Integer.MAX_VALUE / 2)) {
stack.put(DURABILITY_REPAIRED, counter + value);
}
}

public static boolean isOvergrown(ItemStack stack) {
return stack.has(DURABILITY_REPAIRED) && stack.get(DURABILITY_REPAIRED) > OVERGROWN_THRESHOLD;
}

/**
* Applies auto repair onto the item in question
* Only call this on the server, not on the client!
*
* @param stack ItemStack to repair
* @param r Any Minecraft Math {@link Random}
*/
public static void tickAutoRepair(ItemStack stack, Random r) {
if (!stack.isDamaged()) return; // Don't handle auto repair if item is fully repaired

if (!stack.has(PrometheumToolSet.DURABILITY_REPAIRED)) {
stack.put(PrometheumToolSet.DURABILITY_REPAIRED, 0);
}

var dmg = stack.getDamage();
var rng = r.nextInt(200);

if (rng != 177) return; // Roll for repair, ignore if roll fails. Number is arbitrary

// Overgrown Items repair faster
int damageToRepair = PrometheumToolSet.isOvergrown(stack) ? 2 : 1;

// Extra repair speed if bound
if (stack.isIn(MythicTags.PROMETHEUM_ARMOR) && EnchantmentHelper.hasBindingCurse(stack)) {
damageToRepair += 1;
}

int newDamage = MathHelper.clamp(dmg - damageToRepair, 0, Integer.MAX_VALUE);
stack.setDamage(newDamage);

PrometheumToolSet.incrementRepairCounter(stack, damageToRepair);
}
}
Loading

0 comments on commit f7e5be9

Please sign in to comment.