Finish implementing the Ihan Crystal

This commit is contained in:
Zontreck 2024-02-15 19:42:57 -07:00
parent bf89c43f33
commit de99316ca5
4 changed files with 104 additions and 4 deletions

View file

@ -1,11 +1,98 @@
package dev.zontreck.otemod.items;
import dev.zontreck.libzontreck.util.ChatHelpers;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import java.util.List;
public class IhanCrystal extends Item{
public IhanCrystal(Properties p_41383_) {
super(p_41383_);
public static final String TAG_XP = "xp";
public IhanCrystal() {
super(new Properties().stacksTo(1));
}
private void assertTag(ItemStack stack)
{
if(!stack.hasTag() || stack.getTag()==null)
{
stack.setTag(new CompoundTag());
}
}
@Override
public boolean isFoil(ItemStack stack) {
assertTag(stack);
if(stack.getTag().contains(TAG_XP))
{
if(stack.getTag().getInt(TAG_XP) > 0)
{
return true;
}
}
return false;
}
@Override
public void appendHoverText(ItemStack stack, Level p_41422_, List<Component> tooltip, TooltipFlag p_41424_) {
assertTag(stack);
tooltip.add(ChatHelpers.macro("!Dark_Green!Stored XP: !Dark_Red!" + stack.getTag().getInt(TAG_XP) + "!Dark_Green! level" + ((stack.getTag().getInt(TAG_XP)>1)?"levels" : "level")));
}
@Override
public InteractionResultHolder<ItemStack> use(Level serverWorld, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
assertTag(stack);
serverWorld.playSound(player, player.getOnPos(), SoundEvents.EXPERIENCE_ORB_PICKUP, SoundSource.NEUTRAL);
if(!serverWorld.isClientSide)
{
ServerPlayer serverPlayer = (ServerPlayer) player;
if(player.isCrouching())
{
int lvls=0;
// Unpack the experience levels - 10 at a time!!
int xp = stack.getTag().getInt(TAG_XP);
if(xp>10){
lvls = 10;
}else lvls= xp;
xp-=lvls;
serverPlayer.setExperienceLevels(serverPlayer.experienceLevel + lvls);
stack.getTag().putInt(TAG_XP, xp);
} else {
// Store the xp
int xp = stack.getTag().getInt(TAG_XP);
xp += player.experienceLevel;
serverPlayer.setExperienceLevels(0);
stack.getTag().putInt(TAG_XP, xp);
}
}
player.awardStat(Stats.ITEM_USED.get(this));
return InteractionResultHolder.sidedSuccess(stack, serverWorld.isClientSide);
}
}

View file

@ -16,7 +16,7 @@ public class ModItems {
public static final RegistryObject<Item> ETERNIUM_FRAGMENT = CreativeModeTabs.addToOTEModTab(ITEMS.register("eternium_fragment", () -> new Item(new Item.Properties())));
public static final RegistryObject<Item> IHAN_CRYSTAL = CreativeModeTabs.addToOTEModTab(ITEMS.register("ihan_crystal", () -> new Item(new Item.Properties())));
public static final RegistryObject<Item> IHAN_CRYSTAL = CreativeModeTabs.addToOTEModTab(ITEMS.register("ihan_crystal", () -> new IhanCrystal()));

View file

@ -0,0 +1,13 @@
package dev.zontreck.otemod.util;
public class ExperienceUtils
{
public static int getXpNeededForNextLevel(int xpLvl) {
if (xpLvl >= 30) {
return 112 + (xpLvl - 30) * 9;
} else {
return xpLvl >= 15 ? 37 + (xpLvl - 15) * 5 : 7 + xpLvl * 2;
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 206 B

After

Width:  |  Height:  |  Size: 208 B