Implement Consumption Mending

This commit is contained in:
Tara Piccari 2024-01-28 18:41:53 -07:00
parent 40a3ae346e
commit 600b3c686a
7 changed files with 220 additions and 33 deletions

View file

@ -0,0 +1,158 @@
package dev.zontreck.otemod.enchantments;
import dev.zontreck.libzontreck.util.ItemUtils;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.TagKey;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.MendingEnchantment;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.server.ServerLifecycleHooks;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ConsumptionMending extends Enchantment
{
protected ConsumptionMending(EquipmentSlot... slots) {
super(Rarity.RARE, EnchantmentCategory.BREAKABLE, slots);
}
@Override
public int getMaxLevel() {
return 1;
}
@Override
public boolean isTradeable() {
return true;
}
@Override
public boolean isTreasureOnly() {
return false;
}
@Override
public int getMinCost(int pLevel) {
return 25 + (pLevel-1);
}
@Override
public int getMaxCost(int pLevel) {
return pLevel * 23;
}
private static List<ItemStack> append(ServerPlayer player, List<ItemStack> items)
{
List<ItemStack> enchanted = new ArrayList<>();
for(ItemStack stack : player.getInventory().items)
{
if(ItemUtils.getEnchantmentLevel(ModEnchantments.CONSUMPTION_MENDING.get(), stack) > 0)
{
enchanted.add(stack);
}
}
return enchanted;
}
public static void onEntityTick(ServerPlayer player)
{
// Check what items have this enchantment
// If any, check for like-items that lack the enchant.
List<ItemStack> enchanted = new ArrayList<>();
enchanted.addAll(append(player, player.getInventory().items));
enchanted.addAll(append(player, player.getInventory().armor));
List<ItemStack> procList = new ArrayList<>();
procList.addAll(append(player, player.getInventory().offhand));
procList.addAll(append(player, player.getInventory().items));
for(ItemStack stack : enchanted)
{
for(ItemStack item : player.getInventory().items)
{
// Is this a like item, and does it have the enchant?
boolean eligible = false;
if(!(ItemUtils.getEnchantmentLevel(ModEnchantments.CONSUMPTION_MENDING.get(), item)>0))
{
eligible=true;
if(!item.isDamageableItem())
{
eligible=false;
}
}
if(stack.getDamageValue()==0)
{
eligible=false;
}
if(eligible)
{
// Let's eat
int iDamage = stack.getDamageValue();
int iMax = stack.getMaxDamage();
int iNeeds = iDamage;
// The stack we are inspecting:
int nDamage = item.getDamageValue();
int nMax = item.getMaxDamage();
int iRemain = nMax - nDamage;
if(iRemain == iNeeds)
{
nDamage = nMax;
iDamage=0;
} else {
if(iRemain > iNeeds)
{
iDamage -= iNeeds;
nDamage += iNeeds;
} else {
iDamage -= iRemain;
nDamage = nMax;
}
}
if(nDamage == nMax){
// Check for curses on the item
if(item.isEnchanted())
{
Map<Enchantment, Integer> enchantments = ItemUtils.getEnchantments(item);
for(Map.Entry<Enchantment,Integer> entry : enchantments.entrySet())
{
Enchantment id = entry.getKey();
if(id.isCurse())
{
stack.enchant(id, entry.getValue());
player.getLevel().playSound(null, player.getOnPos(), SoundEvents.ANVIL_USE, SoundSource.NEUTRAL,1, player.getRandom().nextFloat(0,1));
}
}
}
item.shrink(1);
player.getLevel().playSound(null, player.getOnPos(), SoundEvents.PLAYER_BURP, SoundSource.NEUTRAL,1, player.getRandom().nextFloat(0,1));
}
else item.setDamageValue(nDamage);
stack.setDamageValue(iDamage);
}
}
}
}
}

View file

@ -0,0 +1,39 @@
package dev.zontreck.otemod.enchantments;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.server.ServerLifecycleHooks;
@Mod.EventBusSubscriber
public class EnchantmentEvents
{
private static boolean canTick = false;
@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event)
{
if(event.phase == TickEvent.Phase.START)
{
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
canTick = server!=null && server.getTickCount()%20==0;
}
}
@SubscribeEvent
public static void onTick(LivingEvent.LivingUpdateEvent tick)
{
if(canTick)
{
// Process Enchantments
if(tick.getEntityLiving() instanceof ServerPlayer sp)
{
FlightEnchantment.runEntityTick(sp);
ConsumptionMending.onEntityTick(sp);
}
}
}
}

View file

@ -73,44 +73,28 @@ public class FlightEnchantment extends Enchantment
}
public static AtomicInteger TICKS = new AtomicInteger(0);
@SubscribeEvent
public static void onEnchantmentTick(TickEvent.PlayerTickEvent event)
public static void runEntityTick(ServerPlayer player)
{
if(event.side == LogicalSide.CLIENT) return;
if(TICKS.getAndIncrement() >= (10*20))
if(OTEServerConfig.DEBUG.get())
{
TICKS.set(0);
if(OTEServerConfig.DEBUG.get())
{
OTEMod.LOGGER.info("> Flight Enchantment Tick <");
}
if(event.phase == TickEvent.Phase.END)
{
ServerPlayer sp = (ServerPlayer) event.player;
ItemStack feet = sp.getItemBySlot(EquipmentSlot.FEET);
boolean hasFlight = false;
if(ItemUtils.getEnchantmentLevel(ModEnchantments.FLIGHT_ENCHANTMENT.get(), feet)>0)hasFlight=true;
if(hasFlight)
{
MobEffectInstance inst = new MobEffectInstance(ModEffects.FLIGHT.get(), 30*20, 0, false, false, true);
event.player.addEffect(inst);
}
}
OTEMod.LOGGER.info("> Flight Enchantment Tick <");
}
ItemStack feet = player.getItemBySlot(EquipmentSlot.FEET);
boolean hasFlight = false;
if(ItemUtils.getEnchantmentLevel(ModEnchantments.FLIGHT_ENCHANTMENT.get(), feet)>0)hasFlight=true;
if(hasFlight)
{
MobEffectInstance inst = new MobEffectInstance(ModEffects.FLIGHT.get(), 30*20, 0, false, false, true);
player.addEffect(inst);
}
}
}

View file

@ -22,6 +22,8 @@ public class ModEnchantments {
public static final RegistryObject<Enchantment> NIGHT_VISION_ENCHANT = REGISTERS.register("night_vision", ()->new NightVisionEnchantment(EquipmentSlot.HEAD));
public static final RegistryObject<Enchantment> CONSUMPTION_MENDING = REGISTERS.register("consumption_mending", ()->new ConsumptionMending(EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.FEET, EquipmentSlot.LEGS, EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND));
public static void register(IEventBus bus){
REGISTERS.register(bus);
}

View file

@ -50,7 +50,7 @@ public class PossBallItem extends Item
ThrownPossBall TPB = new ThrownPossBall(pLevel, pPlayer);
if(pPlayer.getAbilities().instabuild) TPB.setItem(stack.copy());
else
TPB.setItem(stack);
TPB.setItem(stack.copy());
TPB.shootFromRotation(pPlayer, pPlayer.getXRot(), pPlayer.getYRot(), 0.0F, 1.5F, 1.0F);
pLevel.addFreshEntity(TPB);
}

View file

@ -85,6 +85,7 @@ public class ThrownPossBall extends ThrowableItemProjectile
le.save(tag);
getItem().getTag().put("entity", tag);
getItem().setCount(1);
captured=true;
LoreContainer cont = new LoreContainer(getItem());

View file

@ -87,6 +87,9 @@
"enchantment.otemod.borrowed_protection": "Borrowed Protection",
"enchantment.otemod.borrowed_protection.desc": "Borrows protection from any nearby player.",
"enchantment.otemod.night_vision": "Night Vision",
"enchantment.otemod.night_vision.desc": "See in the dark!",
"enchantment.otemod.consumption_mending": "Consumption Mending",
"enchantment.otemod.consumption_mending.desc": "Consumes items to repair durability. May consume curses.",
"effect.otemod.flight": "Flight",