Skip to content

Commit

Permalink
finalized reforger
Browse files Browse the repository at this point in the history
  • Loading branch information
Globox1997 committed Jun 20, 2022
1 parent 55573d5 commit 98fa784
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
45 changes: 27 additions & 18 deletions src/main/java/draylar/tiered/Tiered.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -147,35 +148,41 @@ public static void registerAttributeSyncer() {
});
}

// Todo: Update all nbt tags here!

public static void updateItemStackNbt(PlayerInventory playerInventory) {
for (int u = 0; u < playerInventory.size(); u++) {
ItemStack itemStack = playerInventory.getStack(u);
if (!itemStack.isEmpty() && itemStack.getSubNbt(Tiered.NBT_SUBTAG_KEY) != null) {
// attempt to get a random tier
Identifier potentialAttributeID = ModifierUtils.getRandomAttributeIDFor(itemStack.getItem(), false);

// Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().forEach((id, attribute) -> {
// if (attribute.isValid(Registry.ITEM.getId(item)) && attribute.getWeight() > 0) {
// potentialAttributes.add(new Identifier(attribute.getID()));
// attributeWeights.add(attribute.getWeight());
// }
// });
// Check if attribute exists
List<String> attributeIds = new ArrayList<>();
Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().forEach((id, attribute) -> {
if (attribute.isValid(Registry.ITEM.getId(itemStack.getItem())))
attributeIds.add(attribute.getID());

});
Identifier attributeID = null;
for (int i = 0; i < attributeIds.size(); i++) {
if (itemStack.getSubNbt(Tiered.NBT_SUBTAG_KEY).asString().contains(attributeIds.get(i))) {
attributeID = new Identifier(attributeIds.get(i));
break;
} else if (i == attributeIds.size() - 1) {
ModifierUtils.removeItemStackAttribute(itemStack);
attributeID = ModifierUtils.getRandomAttributeIDFor(itemStack.getItem(), false);
}
}

// System.out.println(itemStack.getSubNbt(Tiered.NBT_SUBTAG_KEY) + " : " + potentialAttributeID);
// found an ID
if (potentialAttributeID != null) {
if (attributeID != null) {

HashMap<String, Object> nbtMap = Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().get(new Identifier(potentialAttributeID.toString())).getNbtValues();
HashMap<String, Object> nbtMap = Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().get(new Identifier(attributeID.toString())).getNbtValues();
// update durability nbt

List<AttributeTemplate> attributeList = Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().get(new Identifier(potentialAttributeID.toString())).getAttributes();
List<AttributeTemplate> attributeList = Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().get(new Identifier(attributeID.toString())).getAttributes();
for (int i = 0; i < attributeList.size(); i++)
if (attributeList.get(i).getAttributeTypeID().equals("tiered:generic.durable")) {
if (nbtMap == null)
nbtMap = new HashMap<String, Object>();
nbtMap.put("durable", attributeList.get(i).getEntityAttributeModifier().getValue());
nbtMap.put("durable", (double) Math.round(attributeList.get(i).getEntityAttributeModifier().getValue() * 100.0) / 100.0);
break;
}

Expand All @@ -197,13 +204,15 @@ else if (value instanceof Double) {
if ((double) value % 1.0 < 0.0001D)
nbtCompound.putInt(key, (int) Math.round((double) value));
else
nbtCompound.putDouble(key, (double) value);
nbtCompound.putDouble(key, Math.round((double) value * 100.0) / 100.0);
}
}
itemStack.setNbt(nbtCompound);

// System.out.println(nbtCompound);
}
if (itemStack.getSubNbt(Tiered.NBT_SUBTAG_KEY) == null)
itemStack.getOrCreateSubNbt(Tiered.NBT_SUBTAG_KEY).putString(Tiered.NBT_SUBTAG_DATA_KEY, attributeID.toString());

playerInventory.setStack(u, itemStack);
}
}
}
Expand Down
45 changes: 38 additions & 7 deletions src/main/java/draylar/tiered/api/ModifierUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package draylar.tiered.api;

import draylar.tiered.Tiered;
import draylar.tiered.config.ConfigInit;
import draylar.tiered.util.SortList;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -32,13 +33,18 @@ public static Identifier getRandomAttributeIDFor(Item item, boolean reforge) {
// collect all valid attributes for the given item and their weights

Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().forEach((id, attribute) -> {
if (attribute.isValid(Registry.ITEM.getId(item)) && attribute.getWeight() > 0) {
if (attribute.isValid(Registry.ITEM.getId(item)) && (attribute.getWeight() > 0 || reforge)) {
potentialAttributes.add(new Identifier(attribute.getID()));
attributeWeights.add(attribute.getWeight());
attributeWeights.add(reforge ? attribute.getWeight() + 1 : attribute.getWeight());
}
});

// return a random attribute if there are any, or null if there are none
if (reforge && attributeWeights.size() > 2) {
SortList.concurrentSort(attributeWeights, attributeWeights, potentialAttributes);
int maxWeight = attributeWeights.get(attributeWeights.size() - 1);
for (int i = 0; i < attributeWeights.size(); i++)
if (attributeWeights.get(i) > maxWeight / 2)
attributeWeights.set(i, (int) (attributeWeights.get(i) * ConfigInit.CONFIG.reforge_modifier));// * attributeWeights;
}
if (potentialAttributes.size() > 0) {
int totalWeight = 0;
for (Integer weight : attributeWeights)
Expand All @@ -57,7 +63,6 @@ public static Identifier getRandomAttributeIDFor(Item item, boolean reforge) {
return null;
}

// Set on
public static void setItemStackAttribute(ItemStack stack, boolean reforge) {
if (stack.getSubNbt(Tiered.NBT_SUBTAG_KEY) == null) {

Expand All @@ -75,7 +80,7 @@ public static void setItemStackAttribute(ItemStack stack, boolean reforge) {
if (attributeList.get(i).getAttributeTypeID().equals("tiered:generic.durable")) {
if (nbtMap == null)
nbtMap = new HashMap<String, Object>();
nbtMap.put("durable", attributeList.get(i).getEntityAttributeModifier().getValue());
nbtMap.put("durable", (double) Math.round(attributeList.get(i).getEntityAttributeModifier().getValue() * 100.0) / 100.0);
break;
}
// add nbtMap
Expand All @@ -96,7 +101,7 @@ else if (value instanceof Double) {
if ((double) value % 1.0 < 0.0001D)
nbtCompound.putInt(key, (int) Math.round((double) value));
else
nbtCompound.putDouble(key, (double) value);
nbtCompound.putDouble(key, Math.round((double) value * 100.0) / 100.0);
}
}
stack.setNbt(nbtCompound);
Expand All @@ -105,6 +110,32 @@ else if (value instanceof Double) {
}
}

public static void removeItemStackAttribute(ItemStack itemStack) {
if (itemStack.hasNbt() && itemStack.getSubNbt(Tiered.NBT_SUBTAG_KEY) != null) {

Identifier tier = new Identifier(itemStack.getOrCreateSubNbt(Tiered.NBT_SUBTAG_KEY).getString(Tiered.NBT_SUBTAG_DATA_KEY));

HashMap<String, Object> nbtMap = Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().get(tier).getNbtValues();
List<String> nbtKeys = new ArrayList<String>();
if (nbtMap != null)
nbtKeys.addAll(nbtMap.keySet().stream().toList());

List<AttributeTemplate> attributeList = Tiered.ATTRIBUTE_DATA_LOADER.getItemAttributes().get(tier).getAttributes();
for (int i = 0; i < attributeList.size(); i++)
if (attributeList.get(i).getAttributeTypeID().equals("tiered:generic.durable")) {
nbtKeys.add("durable");
break;
}

if (!nbtKeys.isEmpty())
for (int i = 0; i < nbtKeys.size(); i++)
if (!nbtKeys.get(i).equals("Damage"))
itemStack.getNbt().remove(nbtKeys.get(i));

itemStack.removeSubNbt(Tiered.NBT_SUBTAG_KEY);
}
}

private ModifierUtils() {
// no-op
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/draylar/tiered/config/TieredConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@Config.Gui.Background("minecraft:textures/block/stone.png")
public class TieredConfig implements ConfigData {

public int test0 = 0;
public int test1 = 0;
@Comment("Items in for example mineshaft chests get modifiers")
public boolean lootContainerModifier = true;
@Comment("Decreases the biggest weights")
public float reforge_modifier = 0.9F;

public int xIconPosition = 0;
public int yIconPosition = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldEvents;

Expand Down Expand Up @@ -76,7 +74,7 @@ public void onContentChanged(Inventory inventory) {

private void updateResult() {
if (this.getSlot(0).hasStack() && this.getSlot(1).hasStack() && this.getSlot(2).hasStack()) {
if (ModifierUtils.getRandomAttributeIDFor(this.getSlot(1).getStack().getItem(), false) != null) {
if (ModifierUtils.getRandomAttributeIDFor(this.getSlot(1).getStack().getItem(), false) != null && !this.getSlot(1).getStack().isDamaged()) {
if (this.getSlot(1).getStack().getItem() instanceof ToolItem)
this.reforgeReady = ((ToolItem) this.getSlot(1).getStack().getItem()).getMaterial().getRepairIngredient().test(this.getSlot(0).getStack());
else if (this.getSlot(1).getStack().getItem() instanceof ArmorItem)
Expand Down Expand Up @@ -157,10 +155,9 @@ private boolean canUse(BlockState state) {
}

public void reforge() {
// this.player.world.playSound(null, pos, SoundEvents.BLOCK_ANVIL_USE, SoundCategory.BLOCKS, 1.0f, 1.0f);
ItemStack itemStack = this.getSlot(1).getStack();
if (itemStack.hasNbt() && itemStack.getSubNbt(Tiered.NBT_SUBTAG_KEY) != null)
itemStack.removeSubNbt(Tiered.NBT_SUBTAG_KEY);
ModifierUtils.removeItemStackAttribute(itemStack);
ModifierUtils.setItemStackAttribute(itemStack, true);

this.decrementStack(0);
this.decrementStack(2);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/tiered/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"text.autoconfig.tiered.option.lootContainerModifier": "Loot Container Modifier",
"text.autoconfig.tiered.option.xIconPosition": "Icon X Position",
"text.autoconfig.tiered.option.yIconPosition": "Icon Y Position",
"text.autoconfig.tiered.option.reforge_modifier": "Reforge Modifier",

"container.reforge": "Reforge Gear",
"screen.tiered.reforging_screen": "Reforge Gear"
Expand Down

0 comments on commit 98fa784

Please sign in to comment.