Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20+build.1
loader_version=0.15.5

# Mod Properties
mod_version=1.3.4_1.20-1.20.4
mod_version=1.3.5_1.20-1.20.4
maven_group=com.megatrex4
archives_base_name=inventory-weight

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/megatrex4/InventoryWeightHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void removeAttributes(PlayerEntity player) {
.removeModifier(SPEED_MODIFIER_UUID);
player.getAttributes().getCustomInstance(EntityAttributes.GENERIC_ATTACK_SPEED)
.removeModifier(ATTACK_SPEED_MODIFIER_UUID);
player.getAttributes().getCustomInstance(EntityAttributes.GENERIC_ARMOR)
player.getAttributes().getCustomInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE)
.removeModifier(DAMAGE_REDUCTION_MODIFIER_UUID);
}

Expand Down Expand Up @@ -188,7 +188,7 @@ private static void removeOverloadEffect(ServerPlayerEntity player) {
.removeModifier(OverloadEffect.SPEED_MODIFIER_UUID);
player.getAttributes().getCustomInstance(EntityAttributes.GENERIC_ATTACK_SPEED)
.removeModifier(OverloadEffect.ATTACK_SPEED_MODIFIER_UUID);
player.getAttributes().getCustomInstance(EntityAttributes.GENERIC_ARMOR)
player.getAttributes().getCustomInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE)
.removeModifier(OverloadEffect.DAMAGE_REDUCTION_MODIFIER_UUID);

player.removeStatusEffect(OVERLOAD_EFFECT);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/megatrex4/commands/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.megatrex4.InventoryWeightHandler;
import com.megatrex4.config.ItemWeightsConfigServer;
import com.megatrex4.util.ItemWeights;
import com.megatrex4.util.ItemCategory;
import com.megatrex4.data.PlayerDataHandler;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
Expand Down Expand Up @@ -154,10 +155,10 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
source.sendFeedback(() -> Text.translatable("command.debugweight", itemIdString, weight), false);
} else {
PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String itemCategory = categoryInfo.getCategory();
ItemCategory itemCategory = categoryInfo.getCategory();
float fallbackWeight = ItemWeights.getItemWeight(categoryInfo.getStack());

source.sendFeedback(() -> Text.translatable("command.debugweight.fallback", itemCategory, fallbackWeight), false);
source.sendFeedback(() -> Text.translatable("command.debugweight.fallback", itemCategory.getName(), fallbackWeight), false);
}

return 1;
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/com/megatrex4/compat/InventoryWeightConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.megatrex4.config.ItemWeightConfigItems;
import com.megatrex4.config.ItemWeightsConfigServer;
import com.megatrex4.util.ItemWeights;
import com.megatrex4.util.ItemCategory;
import com.megatrex4.InventoryWeightState;

import java.util.Map;
Expand Down Expand Up @@ -110,39 +111,39 @@ public static Screen getConfigScreen(Screen parent) {
.build());

// Add fields for item group settings
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.buckets"), ItemWeights.getItemWeight("buckets"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.buckets"), ItemWeights.getItemWeight(ItemCategory.BUCKETS))
.setDefaultValue(ItemWeights.BUCKETS)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("buckets", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.BUCKETS, newValue))
.build());

serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.creative"), ItemWeights.getItemWeight("creative"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.creative"), ItemWeights.getItemWeight(ItemCategory.CREATIVE))
.setDefaultValue(ItemWeights.CREATIVE)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("creative", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.CREATIVE, newValue))
.build());

serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.bottles"), ItemWeights.getItemWeight("bottles"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.bottles"), ItemWeights.getItemWeight(ItemCategory.BOTTLES))
.setDefaultValue(ItemWeights.BOTTLES)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("bottles", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.BOTTLES, newValue))
.build());

serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.blocks"), ItemWeights.getItemWeight("blocks"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.blocks"), ItemWeights.getItemWeight(ItemCategory.BLOCKS))
.setDefaultValue(ItemWeights.BLOCKS)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("blocks", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.BLOCKS, newValue))
.build());

serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.ingots"), ItemWeights.getItemWeight("ingots"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.ingots"), ItemWeights.getItemWeight(ItemCategory.INGOTS))
.setDefaultValue(ItemWeights.INGOTS)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("ingots", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.INGOTS, newValue))
.build());

serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.nuggets"), ItemWeights.getItemWeight("nuggets"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.nuggets"), ItemWeights.getItemWeight(ItemCategory.NUGGETS))
.setDefaultValue(ItemWeights.NUGGETS)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("nuggets", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.NUGGETS, newValue))
.build());

serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.items"), ItemWeights.getItemWeight("items"))
serverCategory.addEntry(entryBuilder.startFloatField(Text.translatable("option.inventoryweight.server.items"), ItemWeights.getItemWeight(ItemCategory.ITEMS))
.setDefaultValue(ItemWeights.ITEMS)
.setSaveConsumer(newValue -> ItemWeights.setItemWeight("items", newValue))
.setSaveConsumer(newValue -> ItemWeights.setItemWeight(ItemCategory.ITEMS, newValue))
.build());

// Items Config Category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.megatrex4.util.ItemWeights;
import com.megatrex4.util.NbtWeightHandler;

import com.megatrex4.util.ItemCategory;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -58,7 +60,7 @@ public static void saveConfig() {

// Change visibility of this method from private to protected
public static boolean isDynamicItem(String itemName) {
return !ItemWeights.isStaticItem(itemName);
return !ItemWeights.isStaticItem(ItemCategory.fromName(itemName));
}

private static JsonObject createDefaultConfig() {
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/megatrex4/data/PlayerDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import com.megatrex4.util.ItemCategory;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
Expand Down Expand Up @@ -72,22 +73,22 @@ public static float getPlayerMaxWeightWithMultiplier(ServerPlayerEntity player)

public static ItemCategoryInfo getItemCategoryInfo(ItemStack stack) {
String itemId = Registries.ITEM.getId(stack.getItem()).toString().toLowerCase();
String category;
ItemCategory category;

if (isCreative(itemId)) {
category = "creative";
category = ItemCategory.CREATIVE;
} else if (itemId.contains("bucket")) {
category = "buckets";
category = ItemCategory.BUCKETS;
} else if (itemId.contains("bottle") || itemId.contains("potion")) {
category = "bottles";
category = ItemCategory.BOTTLES;
} else if (itemId.contains("ingot") || itemId.contains("alloy") || itemId.contains("gem") || itemId.contains("shard")) {
category = "ingots";
category = ItemCategory.INGOTS;
} else if (itemId.contains("nugget")) {
category = "nuggets";
category = ItemCategory.NUGGETS;
} else if (isBlock(stack)) {
category = "blocks";
category = ItemCategory.BLOCKS;
} else {
category = "items";
category = ItemCategory.ITEMS;
}

return new ItemCategoryInfo(stack, category);
Expand All @@ -114,9 +115,9 @@ public static boolean isBlock(ItemStack stack) {

public static class ItemCategoryInfo {
private final ItemStack stack;
private final String category;
private final ItemCategory category;

public ItemCategoryInfo(ItemStack stack, String category) {
public ItemCategoryInfo(ItemStack stack, ItemCategory category) {
this.stack = stack;
this.category = category;
}
Expand All @@ -125,7 +126,7 @@ public ItemStack getStack() {
return stack;
}

public String getCategory() {
public ItemCategory getCategory() {
return category;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ private static BackpackWeightResult calculateTravelersBackpackWeight(NbtCompound
}

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand Down Expand Up @@ -204,7 +203,6 @@ private static BackpackWeightResult calculateScoutWeightFromNbtList(NbtList item
}

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand Down Expand Up @@ -240,7 +238,6 @@ private static BackpackWeightResult calculateToolboxWeight(NbtCompound backpackT
}

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand Down Expand Up @@ -275,7 +272,6 @@ private static BackpackWeightResult calculateDrawerWeightFromNbtList(NbtList ite
}

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand Down Expand Up @@ -309,7 +305,6 @@ private static BackpackWeightResult calculateInmisWeightFromNbtList(NbtList item
}

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand Down Expand Up @@ -342,7 +337,6 @@ private static BackpackWeightResult PackItUpcalculateWeightFromNbtList(NbtList i
}

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand All @@ -361,7 +355,6 @@ private static BackpackWeightResult calculateWeightFromNbtList(NbtList itemList)
ItemStack itemStack = ItemStack.fromNbt(itemTag);

PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();

float itemWeight = getItemWeight(itemStack) * itemStack.getCount();
totalWeight += Math.max(InventoryWeightUtil.ITEMS, itemWeight / 400);
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/com/megatrex4/util/BlockWeightCalculator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.megatrex4.util;

import com.megatrex4.data.PlayerDataHandler;
import com.megatrex4.util.ItemCategory;
import net.minecraft.block.*;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
Expand All @@ -11,7 +12,7 @@
import static com.megatrex4.util.Rarity.getRarityWeight;

public class BlockWeightCalculator {
public static float calculateBlockWeight(ItemStack stack, String category) {
public static float calculateBlockWeight(ItemStack stack, ItemCategory category) {

if (stack.getItem() instanceof BlockItem) {
Block block = ((BlockItem) stack.getItem()).getBlock();
Expand All @@ -28,13 +29,8 @@ public static float calculateBlockWeight(ItemStack stack, String category) {
float hardness = block.getHardness();
float blastResistance = block.getBlastResistance();
boolean isTransparent = !block.getDefaultState().isOpaque();

float weight;
if ("creative".equalsIgnoreCase(category)) {
weight = InventoryWeightUtil.CREATIVE;
} else {
weight = InventoryWeightUtil.BLOCKS;
}
float weight = category == ItemCategory.CREATIVE ?
InventoryWeightUtil.CREATIVE : InventoryWeightUtil.BLOCKS;

weight += (hardness * 10);
weight += Math.min((blastResistance * 50), 10000);
Expand Down Expand Up @@ -84,7 +80,6 @@ public static ShulkerBoxWeightResult calculateShulkerBoxWeight(ItemStack shulker
NbtCompound itemTag = itemList.getCompound(i);
ItemStack itemStack = ItemStack.fromNbt(itemTag);
PlayerDataHandler.ItemCategoryInfo categoryInfo = PlayerDataHandler.getItemCategoryInfo(itemStack);
String category = categoryInfo.getCategory();
float itemWeight = getItemWeight(itemStack) * itemStack.getCount();

totalWeight += itemWeight / 1000; // With modifier
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/megatrex4/util/ItemCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.megatrex4.util;

public enum ItemCategory {
BUCKETS(InventoryWeightUtil.BUCKETS),
BOTTLES(InventoryWeightUtil.BOTTLES),
BLOCKS(InventoryWeightUtil.BLOCKS),
INGOTS(InventoryWeightUtil.INGOTS),
NUGGETS(InventoryWeightUtil.NUGGETS),
ITEMS(InventoryWeightUtil.ITEMS),
CREATIVE(InventoryWeightUtil.CREATIVE);

private final float baseWeight;

ItemCategory(float baseWeight) {
this.baseWeight = baseWeight;
}

public float getBaseWeight() {
return baseWeight;
}

public String getName() {
return name().toLowerCase();
}

public static ItemCategory fromName(String name) {
for (ItemCategory category : values()) {
if (category.name().equalsIgnoreCase(name)) {
return category;
}
}
return ITEMS;
}
}

19 changes: 11 additions & 8 deletions src/main/java/com/megatrex4/util/ItemWeightCalculator.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.megatrex4.util;

import net.minecraft.item.*;
import com.megatrex4.util.ItemCategory;

import static com.megatrex4.util.Rarity.getRarityWeight;

public class ItemWeightCalculator {

public static float calculateItemWeight(ItemStack stack, String category) {

public static float calculateItemWeight(ItemStack stack, ItemCategory category) {
Item item = stack.getItem();
int maxStackSize = item.getMaxCount();
int maxDurability = stack.getMaxDamage();
Expand Down Expand Up @@ -62,14 +64,15 @@ else if (maxStackSize == 1 && maxDurability > 0) {
return (int) Math.floor(Math.max(weight, 1.0f));
}

private static float getCategoryBaseWeight(String category) {

private static float getCategoryBaseWeight(ItemCategory category) {
return switch (category) {
case "ingots" -> InventoryWeightUtil.INGOTS;
case "nuggets" -> InventoryWeightUtil.NUGGETS;
case "buckets" -> InventoryWeightUtil.BUCKETS;
case "bottles" -> InventoryWeightUtil.BOTTLES;
case "blocks" -> InventoryWeightUtil.BLOCKS;
case "creative" -> InventoryWeightUtil.CREATIVE;
case INGOTS -> InventoryWeightUtil.INGOTS;
case NUGGETS -> InventoryWeightUtil.NUGGETS;
case BUCKETS -> InventoryWeightUtil.BUCKETS;
case BOTTLES -> InventoryWeightUtil.BOTTLES;
case BLOCKS -> InventoryWeightUtil.BLOCKS;
case CREATIVE -> InventoryWeightUtil.CREATIVE;
default -> InventoryWeightUtil.ITEMS;
};
}
Expand Down
Loading
Loading