2025-03-13 13:25:01 +01:00

84 lines
3.8 KiB
Java

package com.jenny.xp2items.items;
import com.jenny.xp2items.config.ServerConfig;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class xpTalisman extends Item {
public xpTalisman(Properties properties) {
super(properties);
}
private static int getPlayerXP(@NotNull ServerPlayer player) {
return Mth.floor(player.experienceProgress * (float) player.getXpNeededForNextLevel());
}
private static void spawnItem(Level level, ServerPlayer player) {
List<Item> itemList = ForgeRegistries.ITEMS.getValues().stream().toList();
int xpNeeded = ServerConfig.C_XP_AMOUNT.get();
while (true) {
int expLevelBefore = player.experienceLevel;
float expProgressBefore = player.experienceProgress;
player.giveExperiencePoints(-xpNeeded);
if (getPlayerXP(player) == 0 && player.experienceLevel == 0) {
player.experienceLevel = expLevelBefore;
player.experienceProgress = expProgressBefore;
break;
} else {
Item item = itemList.get(level.getRandom().nextInt(0, itemList.size()));
level.addFreshEntity(new ItemEntity(level, player.getX(), player.getY(), player.getZ(), new ItemStack(item)));
}
}
}
private static void effects(@NotNull Level level, Player player) {
level.playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.EXPERIENCE_ORB_PICKUP, SoundSource.PLAYERS, 1.0F, 1.0F);
for (int i = 0; i < 9; ++i) {
level.addParticle(ParticleTypes.PORTAL, player.getRandomX(0.5F), player.getRandomY() - (double) 0.25F, player.getRandomZ(0.5F), (player.getRandom().nextDouble() - (double) 0.5F) * (double) 2.0F, -player.getRandom().nextDouble(), (player.getRandom().nextDouble() - (double) 0.5F) * (double) 2.0F);
}
}
private static void spawnItemLevels(Level level, ServerPlayer player) {
List<Item> itemList = ForgeRegistries.ITEMS.getValues().stream().toList();
int itemsPerLevel = ServerConfig.C_ITEM_AMOUNT.get();
int xpPoints = getPlayerXP(player);
for (int i = 1; i <= player.experienceLevel; i++) {
for (int i2 = 1; i2 <= itemsPerLevel; i2++) {
Item item = itemList.get(level.getRandom().nextInt(0, itemList.size()));
level.addFreshEntity(new ItemEntity(level, player.getX(), player.getY(), player.getZ(), new ItemStack(item)));
}
}
player.setExperienceLevels(0);
player.setExperiencePoints(0);
player.giveExperiencePoints(xpPoints);
}
@Override
public InteractionResultHolder<ItemStack> use(@NotNull Level level, @NotNull Player player, @NotNull InteractionHand interactionHand) {
if (!level.isClientSide) {
if (ServerConfig.C_XP_AMOUNT.get() > 0) { // xp amount mode
spawnItem(level, (ServerPlayer) player);
} else { // xp level mode
spawnItemLevels(level, (ServerPlayer) player);
}
} else {
effects(level, player);
}
return InteractionResultHolder.success(player.getItemInHand(interactionHand));
}
}