Skip to content

Commit

Permalink
0.7.5 work 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fzzyhmstrs committed Feb 2, 2025
1 parent 1892f27 commit f388197
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import dev.emi.emi.api.stack.EmiStack;
import fzzyhmstrs.emi_loot.EMILoot;
import fzzyhmstrs.emi_loot.util.ConditionalStack;
import fzzyhmstrs.emi_loot.util.LText;
import fzzyhmstrs.emi_loot.util.TextKey;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.EncoderException;
import it.unimi.dsi.fastutil.floats.Float2ObjectArrayMap;
import it.unimi.dsi.fastutil.floats.Float2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2FloatMap;
import it.unimi.dsi.fastutil.objects.Object2FloatOpenHashMap;
import net.minecraft.block.Block;
import net.minecraft.block.BlockTypes;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
Expand All @@ -23,10 +23,9 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

abstract public class AbstractTextKeyParsingClientLootTable<T extends LootReceiver> implements LootReceiver {

Expand All @@ -41,6 +40,7 @@ public AbstractTextKeyParsingClientLootTable(Map<List<TextKey>, ClientRawPool> m
private final Map<List<TextKey>, ClientRawPool> rawItems;
public List<ClientBuiltPool> builtItems;


static Identifier getIdFromBuf(PacketByteBuf buf) {
String idToParse = buf.readString();
if (idToParse.contains(":")) {
Expand All @@ -60,94 +60,107 @@ static Identifier getIdFromBuf(PacketByteBuf buf) {
}
}

abstract List<Pair<Integer, Text>> getSpecialTextKeyList(World world, Block block);
abstract void getSpecialTextKeyList(World world, Block block, List<Pair<Integer, Text>> inputList);

public void build(World world, Block block) {
Map<List<Pair<Integer, Text>>, Object2FloatMap<ItemStack>> builderItems = new HashMap<>();
boolean bl = Registries.BLOCK.getEntry(block).isIn(BlockTags.SLABS);
Map<List<Pair<Integer, Text>>, Object2FloatMap<ItemStack>> builderItems = new LinkedHashMap<>();
rawItems.forEach((list, pool)-> {
List<Pair<Integer, Text>> applyToAllList = new ArrayList<>(getSpecialTextKeyList(world, block));
list.forEach((textKey) -> {
Text text = textKey.process(ItemStack.EMPTY, world).text();
List<Pair<Integer, Text>> applyToAllList = new ArrayList<>();
getSpecialTextKeyList(world, block, applyToAllList);
for (TextKey textKey: list) {
Text text = textKey.processText();
applyToAllList.add(new Pair<>(textKey.index(), text));
});
}
if (bl) System.out.println(applyToAllList);
if (bl) System.out.println();
pool.map().forEach((poolList, poolItemMap)-> {
if (bl) System.out.println(poolList);
if (bl) System.out.println(poolItemMap);
if (bl) System.out.println();
List<Pair<Integer, Text>> newPoolList = new ArrayList<>();
Object2FloatMap<ItemStack> itemsToAdd = new Object2FloatOpenHashMap<>();
List<ItemStack> itemsToRemove = new ArrayList<>();

poolList.forEach((textKey) -> {
for (TextKey textKey : poolList) {
poolItemMap.forEach((poolStack, weight)-> {
List<ItemStack> stacks = textKey.process(poolStack, world).stacks();
AtomicReference<Float> toAddWeight = new AtomicReference<>(1.0f);
List<ItemStack> stacks = textKey.processStack(poolStack, world);
if (bl) System.out.println(stacks);
float toAddWeight = 1.0f;
if (!stacks.contains(poolStack)) {
itemsToRemove.add(poolStack);
toAddWeight.set(poolItemMap.getFloat(poolStack));
toAddWeight = weight;
}

stacks.forEach(stack-> {
for (ItemStack stack: stacks) {
if(poolItemMap.containsKey(stack)) {
toAddWeight.set(poolItemMap.getFloat(stack));
toAddWeight = poolItemMap.getFloat(stack);
}
});
stacks.forEach(stack-> {
}
for (ItemStack stack: stacks) {
if(!poolItemMap.containsKey(stack)) {
itemsToAdd.put(stack, (float)toAddWeight.get());
itemsToAdd.put(stack, toAddWeight);
}
});
}

});
Text text = textKey.process(ItemStack.EMPTY, world).text();
Text text = textKey.processText();
newPoolList.add(new Pair<>(textKey.index(), text));
}

});
list.forEach((textKey) -> {
poolItemMap.forEach((poolStack, weight)-> {
List<ItemStack> stacks = textKey.process(poolStack, world).stacks();
AtomicReference<Float> toAddWeight = new AtomicReference<>(1.0f);
for (TextKey textKey: list) {
poolItemMap.forEach((poolStack, weight) -> {
List<ItemStack> stacks = textKey.processStack(poolStack, world);
float toAddWeight = 1.0f;
if (!stacks.contains(poolStack)) {
itemsToRemove.add(poolStack);
toAddWeight.set(poolItemMap.getFloat(poolStack));
toAddWeight = weight;
}

stacks.forEach(stack-> {
for (ItemStack stack: stacks) {
if(poolItemMap.containsKey(stack)) {
toAddWeight.set(poolItemMap.getFloat(stack));
toAddWeight = poolItemMap.getFloat(stack);
}
});
stacks.forEach(stack-> {
}
for (ItemStack stack: stacks) {
if(!poolItemMap.containsKey(stack)) {
itemsToAdd.put(stack, (float)toAddWeight.get());
itemsToAdd.put(stack, toAddWeight);
}
});

}
});

});
List<Pair<Integer, Text>> summedList = new ArrayList<>(applyToAllList);
summedList.addAll(newPoolList);
if (summedList.isEmpty() && (!EMILoot.config.skippedKeys.contains("emi_loot.no_conditions") || !EMILoot.config.isTooltipStyle())) {
summedList.add(new Pair<>(TextKey.getIndex("emi_loot.no_conditions"), LText.translatable("emi_loot.no_conditions")));
}
List<Pair<Integer, Text>> summedList;
if (applyToAllList.isEmpty()) {
if (newPoolList.isEmpty() && (!EMILoot.config.skippedKeys.contains("emi_loot.no_conditions") || !EMILoot.config.isTooltipStyle())) {
summedList = TextKey.noConditionsList.get();
} else {
summedList = newPoolList;
}
} else {
summedList = new ArrayList<>(applyToAllList);
summedList.addAll(newPoolList);
}

Object2FloatMap<ItemStack> builderPoolMap = builderItems.getOrDefault(summedList, poolItemMap);
builderPoolMap.putAll(itemsToAdd);
itemsToRemove.forEach(builderPoolMap::removeFloat);
builderItems.put(summedList, builderPoolMap);
});

});
List<ClientBuiltPool> finalList = new LinkedList<>();
List<ClientBuiltPool> finalList = new ArrayList<>();
builderItems.forEach((builtList, builtMap)-> {
Float2ObjectMap<List<ItemStack>> consolidatedMap = new Float2ObjectArrayMap<>();
builtMap.forEach((stack, weight)-> {
List<ItemStack> consolidatedList = consolidatedMap.getOrDefault((float)weight, new LinkedList<>());
List<ItemStack> consolidatedList = consolidatedMap.getOrDefault((float)weight, new ArrayList<>());
if (!consolidatedList.contains(stack)) {
consolidatedList.add(stack);
}
consolidatedMap.put((float)weight, consolidatedList);
});
Float2ObjectMap<List<EmiStack>> emiConsolidatedMap = new Float2ObjectArrayMap<>();
consolidatedMap.forEach((consolidatedWeight, consolidatedList)-> {
List<EmiStack> emiStacks = new LinkedList<>();
List<EmiStack> emiStacks = new ArrayList<>();
for (ItemStack i : consolidatedList) {
emiStacks.add(EmiStack.of(i));
}
Expand Down Expand Up @@ -187,20 +200,21 @@ public LootReceiver fromBuf(PacketByteBuf buf, World world) {
return simpleTableToReturn(ids, buf);
}

Map<List<TextKey>, ClientRawPool> itemMap = new HashMap<>();
Map<List<TextKey>, ClientRawPool> itemMap = new LinkedHashMap<>();
//shortcut -1 means a simple table. One guaranteed drop of quantity 1 with no conditions.

for (int b = 0; b < builderCount; b++) {

List<TextKey> qualifierList = new LinkedList<>();

int conditionSize = buf.readShort();

List<TextKey> qualifierList = new ArrayList<>(conditionSize + 1);

for (int i = 0; i < conditionSize; i++) {
try {
TextKey key = TextKey.fromBuf(buf);
qualifierList.add(key);
} catch (DecoderException e) {
EMILoot.LOGGER.error("Client table " + id + " had a TextKey decoding error while reading a loot condition!");
EMILoot.LOGGER.error("Client table {} had a TextKey decoding error while reading a loot condition!", id);
}
}

Expand All @@ -210,24 +224,25 @@ public LootReceiver fromBuf(PacketByteBuf buf, World world) {
TextKey key = TextKey.fromBuf(buf);
qualifierList.add(key);
} catch (DecoderException e) {
EMILoot.LOGGER.error("Client table " + id + " had a TextKey decoding error while reading a loot function!");
EMILoot.LOGGER.error("Client table {} had a TextKey decoding error while reading a loot function!", id);
}
}

ClientRawPool pool = itemMap.getOrDefault(qualifierList, new ClientRawPool(new HashMap<>()));
ClientRawPool pool = itemMap.getOrDefault(qualifierList, new ClientRawPool(new LinkedHashMap<>()));

int pileSize = buf.readShort();
for (int i = 0; i < pileSize; i++) {

List<TextKey> pileQualifierList = new LinkedList<>();

int pileQualifierSize = buf.readShort();

List<TextKey> pileQualifierList = new ArrayList<>(pileQualifierSize);

for (int j = 0; j < pileQualifierSize; j++) {
try {
TextKey key = TextKey.fromBuf(buf);
pileQualifierList.add(key);
} catch (DecoderException e) {
EMILoot.LOGGER.error("Client table " + id + " had a TextKey decoding error while reading an item pile qualifier!");
EMILoot.LOGGER.error("Client table {} had a TextKey decoding error while reading an item pile qualifier!", id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public class ClientBlockLootTable extends AbstractTextKeyParsingClientLootTable<
private static final Identifier EMPTY = Identifier.of("blocks/empty");
public final Identifier id;
public final Identifier blockId;
public final boolean isSimple;

public ClientBlockLootTable() {
super();
this.id = EMPTY;
this.blockId = Identifier.of("air");
this.isSimple = false;
}

public ClientBlockLootTable(Identifier id, Map<List<TextKey>, ClientRawPool> map) {
Expand All @@ -49,6 +51,21 @@ public ClientBlockLootTable(Identifier id, Map<List<TextKey>, ClientRawPool> map
} else {
blockId = Identifier.of(ns, pth.substring(Math.min(lastSlashIndex + 1, pth.length())));
}
this.isSimple = false;
}

public ClientBlockLootTable(Identifier id, Map<List<TextKey>, ClientRawPool> map, boolean isSimple) {
super(map);
this.id = id;
String ns = id.getNamespace();
String pth = id.getPath();
int lastSlashIndex = pth.lastIndexOf('/');
if (lastSlashIndex == -1) {
blockId = Identifier.of(ns, pth);
} else {
blockId = Identifier.of(ns, pth.substring(Math.min(lastSlashIndex + 1, pth.length())));
}
this.isSimple = true;
}

@Override
Expand All @@ -62,7 +79,7 @@ public boolean isEmpty() {
}

@Override
List<Pair<Integer, Text>> getSpecialTextKeyList(World world, Block block) {
void getSpecialTextKeyList(World world, Block block, List<Pair<Integer, Text>> inputList) {
String tool = "";
if (block.getRegistryEntry().isIn(BlockTags.PICKAXE_MINEABLE)) {
tool = "pickaxe";
Expand All @@ -73,7 +90,6 @@ List<Pair<Integer, Text>> getSpecialTextKeyList(World world, Block block) {
} else if (block.getRegistryEntry().isIn(BlockTags.HOE_MINEABLE)) {
tool = "hoe";
}
List<Pair<Integer, Text>> toolNeededList = new LinkedList<>();
if (!Objects.equals(tool, "")) {
String type;
if (block.getRegistryEntry().isIn(BlockTags.NEEDS_STONE_TOOL)) {
Expand All @@ -88,10 +104,9 @@ List<Pair<Integer, Text>> getSpecialTextKeyList(World world, Block block) {
String keyString = "emi_loot." + tool + "." + type;
int keyIndex = TextKey.getIndex(keyString);
if (keyIndex != -1) {
toolNeededList.add(new Pair<>(keyIndex, LText.translatable(keyString)));
inputList.add(new Pair<>(keyIndex, LText.translatable(keyString)));
}
}
return toolNeededList;
}

@Override
Expand All @@ -108,7 +123,7 @@ ClientBlockLootTable simpleTableToReturn(Pair<Identifier, Identifier> ids, Packe
simplePool.map().put(new ArrayList<>(), simpleMap);
Map<List<TextKey>, ClientRawPool> itemMap = new HashMap<>();
itemMap.put(new ArrayList<>(), simplePool);
return new ClientBlockLootTable(ids.getLeft(), itemMap);
return new ClientBlockLootTable(ids.getLeft(), itemMap, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,7 @@ public boolean isEmpty() {
}

@Override
List<Pair<Integer, Text>> getSpecialTextKeyList(World world, Block block) {
String tool = "";
if (block.getRegistryEntry().isIn(BlockTags.PICKAXE_MINEABLE)) {
tool = "pickaxe";
} else if (block.getRegistryEntry().isIn(BlockTags.AXE_MINEABLE)) {
tool = "axe";
} else if (block.getRegistryEntry().isIn(BlockTags.SHOVEL_MINEABLE)) {
tool = "shovel";
} else if (block.getRegistryEntry().isIn(BlockTags.HOE_MINEABLE)) {
tool = "hoe";
}
List<Pair<Integer, Text>> toolNeededList = new LinkedList<>();
if (!Objects.equals(tool, "")) {
String type;
if (block.getRegistryEntry().isIn(BlockTags.NEEDS_STONE_TOOL)) {
type = "stone";
} else if (block.getRegistryEntry().isIn(BlockTags.NEEDS_IRON_TOOL)) {
type = "iron";
} else if (block.getRegistryEntry().isIn(BlockTags.NEEDS_DIAMOND_TOOL)) {
type = "diamond";
} else {
type = "wood";
}
String keyString = "emi_loot." + tool + "." + type;
int keyIndex = TextKey.getIndex(keyString);
if (keyIndex != -1) {
toolNeededList.add(new Pair<>(keyIndex, LText.translatable(keyString)));
}
}
return toolNeededList;
void getSpecialTextKeyList(World world, Block block, List<Pair<Integer, Text>> inputList) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public boolean isEmpty() {
}

@Override
List<Pair<Integer, Text>> getSpecialTextKeyList(World world, Block block) {
return List.of();
void getSpecialTextKeyList(World world, Block block, List<Pair<Integer, Text>> inputList) {
}

@Override
Expand Down
Loading

0 comments on commit f388197

Please sign in to comment.