This commit is contained in:
Jenny committed 2025-03-12 05:42:25 +01:00
commit c65afe83e8
16 files changed
+541

No files matched your search

@@ -0,0 +1,27 @@
package com.jenny.xp2items;
import com.jenny.xp2items.config.ServerConfig;
import com.jenny.xp2items.items.items;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod(Xp2Items.MODID)
public class Xp2Items {
public static final String MODID = "xp2items";
public Xp2Items(FMLJavaModLoadingContext context) {
IEventBus modEventBus = context.getModEventBus();
items.register(modEventBus);
creativeTab.register(modEventBus);
MinecraftForge.EVENT_BUS.register(this);
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ServerConfig.SPEC, "xp2items.toml");
}
}
@@ -0,0 +1,22 @@
package com.jenny.xp2items.config;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.common.Mod;
import static com.jenny.xp2items.Xp2Items.MODID;
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ServerConfig {
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
public static final ForgeConfigSpec SPEC;
public static final ForgeConfigSpec.ConfigValue<Integer> C_XP_AMOUNT;
public static final ForgeConfigSpec.ConfigValue<Integer> C_ITEM_AMOUNT;
static {
C_XP_AMOUNT = BUILDER.comment("xp needed per loot drop, 0=per level").define("xp_amount", 25);
C_ITEM_AMOUNT = BUILDER.comment("items per loot drop").define("item_amount", 1);
SPEC = BUILDER.build();
}
}
@@ -0,0 +1,35 @@
package com.jenny.xp2items;
import com.jenny.xp2items.items.items;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;
import java.util.Arrays;
import static com.jenny.xp2items.Xp2Items.MODID;
public class creativeTab {
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID);
public static final RegistryObject<CreativeModeTab> CREATIVE_TAB = CREATIVE_MODE_TABS.register("xp2items", () -> CreativeModeTab.builder().withTabsBefore(CreativeModeTabs.SPAWN_EGGS).icon(() -> items.XP_TALISMAN.get().getDefaultInstance()).displayItems((parameters, output) -> output.acceptAll(Arrays.stream(getItems()).toList())).title(Component.literal("xp2items")).build());
public static void register(IEventBus bus) {
CREATIVE_MODE_TABS.register(bus);
}
public static ItemStack[] getItems() {
ItemStack[] ret = new ItemStack[items.ITEMS.getEntries().size()];
int i = 0;
for (RegistryObject<Item> item : items.ITEMS.getEntries()) {
ret[i] = item.get().getDefaultInstance();
i++;
}
return ret;
}
}
@@ -0,0 +1,23 @@
package com.jenny.xp2items.items;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import static com.jenny.xp2items.Xp2Items.MODID;
public class items {
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
public static final RegistryObject<Item> XP_TALISMAN = ITEMS.register("xp_talisman", () -> new xpTalisman(new Item.Properties().setId(ResourceKey.create(Registries.ITEM, ResourceLocation.fromNamespaceAndPath(MODID, "xp_talisman"))).stacksTo(1)));
public static void register(IEventBus bus) {
ITEMS.register(bus);
}
}
@@ -0,0 +1,76 @@
package com.jenny.xp2items.items;
import com.jenny.xp2items.config.ServerConfig;
import net.minecraft.server.commands.ExperienceCommand;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.apache.logging.log4j.core.jmx.Server;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class xpTalisman extends Item {
public xpTalisman(Properties properties) {
super(properties);
}
@Override
public @NotNull InteractionResult 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);
}
}
return InteractionResult.SUCCESS;
}
private static void spawnItem(Level level, ServerPlayer player) {
List<Item> itemList = ForgeRegistries.ITEMS.getValues().stream().toList();
int xpNeeded = ServerConfig.C_XP_AMOUNT.get();
System.out.println(getPlayerXP(player));
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 {
System.out.println("item dropped");
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 int getPlayerXP(ServerPlayer player) {
return Mth.floor(player.experienceProgress * (float) player.getXpNeededForNextLevel());
}
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++) {
System.out.println("item dropped");
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);
}
}
+67
View File
@@ -0,0 +1,67 @@
# This is an example mods.toml file. It contains the data relating to the loading mods.
# There are several mandatory fields (#mandatory), and many more that are optional (#optional).
# The overall format is standard TOML format, v0.5.0.
# Note that there are a couple of TOML lists in this file.
# Find more information on toml format here: https://github.com/toml-lang/toml
# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
modLoader = "javafml" #mandatory
logoFile = "logo.png"
# A version range to match for said mod loader - for regular FML @Mod it will be the forge version
loaderVersion = "${loader_version_range}" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties.
# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here.
license = "${mod_license}"
# A URL to refer people to when problems occur with this mod
#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
# If your mod is purely client-side and has no multiplayer functionality (be it dedicated servers or Open to LAN),
# set this to true, and Forge will set the correct displayTest for you and skip loading your mod on dedicated servers.
#clientSideOnly=true #optional - defaults to false if absent
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId = "${mod_id}" #mandatory
# The version number of the mod
version = "${mod_version}" #mandatory
# A display name for the mod
displayName = "${mod_name}" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://docs.minecraftforge.net/en/latest/misc/updatechecker/
#updateJSONURL="https://change.me.example.invalid/updates.json" #optional
# A URL for the "homepage" for this mod, displayed in the mod UI
#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional
# A file name (in the root of the mod JAR) containing a logo for display
#logoFile="xp2items.png" #optional
# A text field displayed in the mod UI
#credits="Thanks for this example mod goes to Java" #optional
# A text field displayed in the mod UI
authors = "${mod_authors}" #optional
# Display Test controls the display for your mod in the server connection screen
# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod.
# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod.
# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component.
# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value.
# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself.
#displayTest="MATCH_VERSION" # if nothing is specified, MATCH_VERSION is the default when clientSideOnly=false, otherwise IGNORE_ALL_VERSION when clientSideOnly=true (#optional)
# The description text for the mod (multi line!) (#mandatory)
description = '''${mod_description}'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies."${mod_id}"]] #optional
# the modid of the dependency
modId = "forge" #mandatory
# Does this dependency have to exist - if not, ordering below must be specified
mandatory = true #mandatory
# The version range of the dependency
versionRange = "${forge_version_range}" #mandatory
# An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory
# BEFORE - This mod is loaded BEFORE the dependency
# AFTER - This mod is loaded AFTER the dependency
ordering = "NONE"
# Side this dependency is applied on - BOTH, CLIENT, or SERVER
side = "BOTH"# Here's another dependency
[[dependencies."${mod_id}"]]
modId = "minecraft"
mandatory = true
# This version range declares a minimum of the current minecraft version up to but not including the next major version
versionRange = "${minecraft_version_range}"
ordering = "NONE"
side = "BOTH"
@@ -0,0 +1,6 @@
{
"model": {
"type": "minecraft:model",
"model": "xp2items:item/xp_talisman"
}
}
@@ -0,0 +1,3 @@
{
"item.xp2items.xp_talisman": "XP Talisman"
}
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "xp2items:item/xp_talisman"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

@@ -0,0 +1,11 @@
{
"type": "minecraft:crafting_shapeless",
"category": "misc",
"ingredients": [
"minecraft:diamond",
"minecraft:ender_pearl"
],
"result": {
"id": "xp2items:xp_talisman"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 528 B

+6
View File
@@ -0,0 +1,6 @@
{
"pack": {
"description": "xp2items resources",
"pack_format": 34
}
}