Skip to content

Commit

Permalink
skills are now capped manually; creeper armor
Browse files Browse the repository at this point in the history
  • Loading branch information
natanmaia95 committed Sep 2, 2024
1 parent 2691308 commit 5b2ad31
Show file tree
Hide file tree
Showing 45 changed files with 594 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.nateplays.my_neoforge_mod.enchantment;

import com.nateplays.my_neoforge_mod.MyNeoForgeMod;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.Level;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public class ModEnchantmentHelper {

public static ResourceLocation getRL(String id) {
return ResourceLocation.fromNamespaceAndPath(MyNeoForgeMod.MODID, id);
}

@Nullable
public static Holder<Enchantment> getEnchantmentFromLocation(ResourceLocation location, Level level) {
HolderLookup.RegistryLookup<Enchantment> registryLookup = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
return getEnchantmentFromLocationAndLookup(location, registryLookup);
}

@Nullable public static Holder<Enchantment> getEnchantmentFromLocationAndLookup(ResourceLocation location, HolderLookup.RegistryLookup<Enchantment> lookup) {
return lookup.getOrThrow(ResourceKey.create(Registries.ENCHANTMENT, location)).getDelegate();
}

public static int getTotalEnchantmentLevel(LivingEntity livingEntity, Holder<Enchantment> enchantment) {
int totalLevel = 0;
for (EquipmentSlot slot : EquipmentSlot.values()) {
ItemStack itemStack = livingEntity.getItemBySlot(slot);
if (enchantment.value().matchingSlot(slot)) {
int level = itemStack.getEnchantmentLevel(enchantment);
totalLevel += level;
}
}
if (enchantment.is(ModEnchantments.TAG_SKILL_ENCHANTMENTS)) {
totalLevel = Mth.floor(Math.min(totalLevel, enchantment.value().getMaxLevel()));
}
return totalLevel;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.nateplays.my_neoforge_mod.enchantment;

import com.nateplays.my_neoforge_mod.MyNeoForgeMod;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.enchantment.Enchantment;

public class ModEnchantments {

public static final TagKey<Enchantment> TAG_SKILL_ENCHANTMENTS = TagKey.create(
Registries.ENCHANTMENT,
// ResourceLocation.fromNamespaceAndPath(MyNeoForgeMod.MODID, "skill_enchantments"
ResourceLocation.fromNamespaceAndPath(MyNeoForgeMod.MODID, "skill_enchantments"
));


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.nateplays.my_neoforge_mod.enchantment.event;

import com.nateplays.my_neoforge_mod.MyNeoForgeMod;
import net.minecraft.world.level.Explosion;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.level.ExplosionEvent;

@EventBusSubscriber(modid = MyNeoForgeMod.MODID)
public class BombardierEventHandler {


@SubscribeEvent
public static void onExplosionDetonate(ExplosionEvent.Detonate event) {
Explosion explosion = event.getExplosion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.nateplays.my_neoforge_mod.enchantment.event;

import com.mojang.logging.LogUtils;
import com.nateplays.my_neoforge_mod.MyNeoForgeMod;
import com.nateplays.my_neoforge_mod.attribute.ModAttributes;
import com.nateplays.my_neoforge_mod.enchantment.ModEnchantmentHelper;
import com.nateplays.my_neoforge_mod.enchantment.ModEnchantments;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.living.LivingEquipmentChangeEvent;
import net.neoforged.neoforge.event.level.ExplosionEvent;
import org.slf4j.Logger;

import java.util.function.Supplier;

@EventBusSubscriber(modid = MyNeoForgeMod.MODID)
public class SkillAttributeEventHandler {

private static final Logger LOGGER = LogUtils.getLogger();

@SubscribeEvent
public static void onLivingEquipmentChange(LivingEquipmentChangeEvent event) {
LivingEntity livingEntity = event.getEntity();
Level level = livingEntity.level();
if (!level.isClientSide) {
updateQuickEatingModifier(livingEntity);
updateMuckResistanceModifier(livingEntity);
}
}



public static void updateQuickEatingModifier(LivingEntity livingEntity) {
String enchantName = "quick_eating";
AttributeInstance attributeInstance = livingEntity.getAttribute(ModAttributes.EATING_SPEED);
if (attributeInstance == null) return;
ResourceLocation modifierId = ResourceLocation.fromNamespaceAndPath(MyNeoForgeMod.MODID, "skill."+enchantName);
Holder<Enchantment> enchantment = ModEnchantmentHelper.getEnchantmentFromLocation(ModEnchantmentHelper.getRL(enchantName), livingEntity.level());
int enchantLevel = ModEnchantmentHelper.getTotalEnchantmentLevel(livingEntity, enchantment);
if (enchantLevel > 0) {
AttributeModifier modifier = new AttributeModifier(modifierId, enchantLevel * 1.0d, AttributeModifier.Operation.ADD_VALUE);
attributeInstance.addOrUpdateTransientModifier(modifier);
} else {
attributeInstance.removeModifier(modifierId);
}
}

public static void updateMuckResistanceModifier(LivingEntity livingEntity) {
String enchantName = "muck_resistance";
AttributeInstance attributeInstance = livingEntity.getAttribute(Attributes.MOVEMENT_EFFICIENCY);
if (attributeInstance == null) return;
ResourceLocation modifierId = ResourceLocation.fromNamespaceAndPath(MyNeoForgeMod.MODID, "skill."+enchantName);
Holder<Enchantment> enchantment = ModEnchantmentHelper.getEnchantmentFromLocation(ModEnchantmentHelper.getRL(enchantName), livingEntity.level());
int enchantLevel = ModEnchantmentHelper.getTotalEnchantmentLevel(livingEntity, enchantment);
if (enchantLevel > 0) {
AttributeModifier modifier = new AttributeModifier(modifierId, enchantLevel * 0.5d, AttributeModifier.Operation.ADD_VALUE);
attributeInstance.addOrUpdateTransientModifier(modifier);
} else {
attributeInstance.removeModifier(modifierId);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public class ModCreativeModeTabs {
outputAcceptHuntingArmorItem(ModArmorItems.HUNTER_CHESTPLATE.get(), itemDisplayParameters, output);
outputAcceptHuntingArmorItem(ModArmorItems.HUNTER_LEGGINGS.get(), itemDisplayParameters, output);
outputAcceptHuntingArmorItem(ModArmorItems.HUNTER_BOOTS.get(), itemDisplayParameters, output);
outputAcceptHuntingArmorItem(ModArmorItems.CREEPER_HELMET.get(), itemDisplayParameters, output);
outputAcceptHuntingArmorItem(ModArmorItems.CREEPER_CHESTPLATE.get(), itemDisplayParameters, output);
outputAcceptHuntingArmorItem(ModArmorItems.CREEPER_LEGGINGS.get(), itemDisplayParameters, output);
outputAcceptHuntingArmorItem(ModArmorItems.CREEPER_BOOTS.get(), itemDisplayParameters, output);
})
.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.nateplays.my_neoforge_mod.MyNeoForgeMod;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;

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

public class ArmorSkillData {
private final ResourceLocation armorType;
private final ResourceLocation armorItem;
private final List<EnchantmentData> enchantments;

public ArmorSkillData(ResourceLocation armorType, List<EnchantmentData> enchantments) {
this.armorType = armorType;
public ArmorSkillData(ResourceLocation armorItem, List<EnchantmentData> enchantments) {
this.armorItem = armorItem;
this.enchantments = enchantments;
}

public ResourceLocation getArmorType() {
return armorType;
public ResourceLocation getArmorItem() {
return armorItem;
}

public List<EnchantmentData> getEnchantments() {
return enchantments;
}

public static ArmorSkillData fromJson(JsonObject json) {
String pathWithNamespace = GsonHelper.getAsString(json, "armor_type");
ResourceLocation armorType = ResourceLocation.parse(pathWithNamespace);
String pathWithNamespace = GsonHelper.getAsString(json, "armor_item");
ResourceLocation armorId = ResourceLocation.parse(pathWithNamespace);
List<EnchantmentData> enchantments = new ArrayList<>();

JsonArray enchantmentsArray = GsonHelper.getAsJsonArray(json, "enchantments");
Expand All @@ -42,7 +41,7 @@ public static ArmorSkillData fromJson(JsonObject json) {
enchantments.add(new EnchantmentData(enchantment, level));
}

return new ArmorSkillData(armorType, enchantments);
return new ArmorSkillData(armorId, enchantments);
}

public static class EnchantmentData {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nateplays.my_neoforge_mod.item.armor;

import com.nateplays.my_neoforge_mod.enchantment.ModEnchantmentHelper;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.Model;
import net.minecraft.client.model.PlayerModel;
Expand Down Expand Up @@ -56,60 +57,27 @@ public static ItemStack makeItemStackWithLookup(HuntingArmorItem item, HolderLoo
}

public static void applyInitialEnchantments(ItemStack stack, Level level) {
ResourceLocation armorTypeId = stack.getItemHolder().getKey().location();
// LOGGER.debug("armorid: " + armorTypeId.toString());
ArmorSkillData data = ArmorSkillDataLoader.getArmorEnchantmentData(armorTypeId);

if (data != null) {
// LOGGER.debug("hasdata");
List<Holder<Enchantment>> holdersList = new ArrayList<>();
level.registryAccess().registryOrThrow(Registries.ENCHANTMENT).holders().forEach(holdersList::add);

// LOGGER.debug(holdersList.toString());

for (ArmorSkillData.EnchantmentData enchantmentData : data.getEnchantments()) {
Optional<Holder<Enchantment>> holderOptional = holdersList.stream()
.filter(h -> h.unwrapKey().isPresent() && h.unwrapKey().get().location().equals(enchantmentData.getEnchantment()))
.findFirst();
if (holderOptional.isPresent()) {
// LOGGER.debug("found thing");
stack.enchant(holderOptional.get(), enchantmentData.getLevel());
}
}
} else {
// LOGGER.debug("not hasdata");
}
HolderLookup.RegistryLookup<Enchantment> registryLookup = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
applyInitialEnchantmentsWithLookup(stack, registryLookup);
}

//copy-pasted because fuck it
public static void applyInitialEnchantmentsWithLookup (ItemStack stack, HolderLookup.RegistryLookup<Enchantment> registryLookup) {
ResourceLocation armorTypeId = stack.getItemHolder().getKey().location();
ResourceLocation armorId = stack.getItemHolder().getKey().location();
// LOGGER.debug("armorid: " + armorTypeId.toString());
ArmorSkillData data = ArmorSkillDataLoader.getArmorEnchantmentData(armorTypeId);
ArmorSkillData data = ArmorSkillDataLoader.getArmorEnchantmentData(armorId);

if (data != null) {
// LOGGER.debug("hasdata");
List<Holder<Enchantment>> holdersList = new ArrayList<>();
registryLookup.listElements().forEach(enchantmentReference -> {
Holder<Enchantment> h = enchantmentReference.getDelegate();
holdersList.add(h);
});

// LOGGER.debug(holdersList.toString());

for (ArmorSkillData.EnchantmentData enchantmentData : data.getEnchantments()) {
Optional<Holder<Enchantment>> holderOptional = holdersList.stream()
.filter(h -> h.unwrapKey().isPresent() && h.unwrapKey().get().location().equals(enchantmentData.getEnchantment()))
.findFirst();
if (holderOptional.isPresent()) {
// LOGGER.debug("found thing");
stack.enchant(holderOptional.get(), enchantmentData.getLevel());
Holder<Enchantment> holder = ModEnchantmentHelper.getEnchantmentFromLocationAndLookup(enchantmentData.getEnchantment(), registryLookup);
if (holder != null) {
stack.enchant(holder, enchantmentData.getLevel());
}
}
} else {
// LOGGER.debug("not hasdata");
}
}

} else LOGGER.debug("not hasdata");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ public class ModArmorItems {
new Item.Properties().durability(100)
));

public static final Supplier<HuntingArmorItem> CREEPER_HELMET = ARMOR_ITEMS.register("creeper_helmet", () -> new HuntingArmorItem(
ModArmorMaterials.CREEPER, ArmorItem.Type.HELMET,
new Item.Properties().durability(150)
));
public static final Supplier<HuntingArmorItem> CREEPER_CHESTPLATE = ARMOR_ITEMS.register("creeper_chestplate", () -> new HuntingArmorItem(
ModArmorMaterials.CREEPER, ArmorItem.Type.CHESTPLATE,
new Item.Properties().durability(150)
));
public static final Supplier<HuntingArmorItem> CREEPER_LEGGINGS = ARMOR_ITEMS.register("creeper_leggings", () -> new HuntingArmorItem(
ModArmorMaterials.CREEPER, ArmorItem.Type.LEGGINGS,
new Item.Properties().durability(150)
));
public static final Supplier<HuntingArmorItem> CREEPER_BOOTS = ARMOR_ITEMS.register("creeper_boots", () -> new HuntingArmorItem(
ModArmorMaterials.CREEPER, ArmorItem.Type.BOOTS,
new Item.Properties().durability(150)
));

public static void register(IEventBus eventBus) {
ARMOR_ITEMS.register(eventBus);
}
Expand Down
Loading

0 comments on commit 5b2ad31

Please sign in to comment.