This commit is contained in:
gnembon committed 2024-10-04 20:16:39 +02:00
1 parent 6144967299
commit 3a66dd0a9b
17 files changed
+269 -232

No files matched your search

+140 -94
View File
@@ -1,11 +1,12 @@
package carpet.helpers;
import carpet.CarpetServer;
import carpet.fakes.RecipeManagerInterface;
import carpet.script.utils.RecipeHelper;
import carpet.utils.Messenger;
import it.unimi.dsi.fastutil.objects.Object2LongLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import net.minecraft.ChatFormatting;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
@@ -21,9 +22,9 @@ import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.PlacementInfo;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AbstractBannerBlock;
import net.minecraft.world.level.block.BeaconBeamBlock;
import net.minecraft.world.level.block.Block;
@@ -36,7 +37,6 @@ import java.util.EnumMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Map.entry;
@@ -101,7 +101,9 @@ public class HopperCounter
this.color = color;
String hexColor = Integer.toHexString(color.getTextColor());
if (hexColor.length() < 6)
{
hexColor = "0".repeat(6 - hexColor.length()) + hexColor;
}
this.coloredName = '#' + hexColor + ' ' + color.getName();
}
@@ -135,6 +137,7 @@ public class HopperCounter
/**
* Resets all counters, clearing their items.
*
* @param fresh Whether or not to start the clocks going immediately or later.
*/
public static void resetAll(MinecraftServer server, boolean fresh)
@@ -142,7 +145,10 @@ public class HopperCounter
for (HopperCounter counter : COUNTERS.values())
{
counter.reset(server);
if (fresh) counter.startTick = -1;
if (fresh)
{
counter.startTick = -1;
}
}
}
@@ -158,7 +164,10 @@ public class HopperCounter
List<Component> temp = counter.format(server, realtime, false);
if (temp.size() > 1)
{
if (!text.isEmpty()) text.add(Messenger.s(""));
if (!text.isEmpty())
{
text.add(Messenger.s(""));
}
text.addAll(temp);
}
}
@@ -180,7 +189,7 @@ public class HopperCounter
{
if (brief)
{
return Collections.singletonList(Messenger.c("b"+coloredName,"w : ","gi -, -/h, - min "));
return Collections.singletonList(Messenger.c("b" + coloredName, "w : ", "gi -, -/h, - min "));
}
return Collections.singletonList(Messenger.c(coloredName, "w hasn't started counting yet"));
}
@@ -189,36 +198,36 @@ public class HopperCounter
{
if (brief)
{
return Collections.singletonList(Messenger.c("b"+coloredName,"w : ","wb 0","w , ","wb 0","w /h, ", String.format("wb %.1f ", ticks / (20.0 * 60.0)), "w min"));
return Collections.singletonList(Messenger.c("b" + coloredName, "w : ", "wb 0", "w , ", "wb 0", "w /h, ", String.format("wb %.1f ", ticks / (20.0 * 60.0)), "w min"));
}
return Collections.singletonList(Messenger.c("w No items for ", coloredName, String.format("w yet (%.2f min.%s)",
ticks / (20.0 * 60.0), (realTime ? " - real time" : "")),
"nb [X]", "^g reset", "!/counter " + color.getName() +" reset"));
ticks / (20.0 * 60.0), (realTime ? " - real time" : "")),
"nb [X]", "^g reset", "!/counter " + color.getName() + " reset"));
}
if (brief)
{
return Collections.singletonList(Messenger.c("b"+coloredName,"w : ",
"wb "+total,"w , ",
"wb "+(total * (20 * 60 * 60) / ticks),"w /h, ",
return Collections.singletonList(Messenger.c("b" + coloredName, "w : ",
"wb " + total, "w , ",
"wb " + (total * (20 * 60 * 60) / ticks), "w /h, ",
String.format("wb %.1f ", ticks / (20.0 * 60.0)), "w min"
));
}
List<Component> items = new ArrayList<>();
items.add(Messenger.c("w Items for ", coloredName,
"w (",String.format("wb %.2f", ticks*1.0/(20*60)), "w min"+(realTime?" - real time":"")+"), ",
"w total: ", "wb "+total, "w , (",String.format("wb %.1f",total*1.0*(20*60*60)/ticks),"w /h):",
"nb [X]", "^g reset", "!/counter "+color+" reset"
"w (", String.format("wb %.2f", ticks * 1.0 / (20 * 60)), "w min" + (realTime ? " - real time" : "") + "), ",
"w total: ", "wb " + total, "w , (", String.format("wb %.1f", total * 1.0 * (20 * 60 * 60) / ticks), "w /h):",
"nb [X]", "^g reset", "!/counter " + color + " reset"
));
items.addAll(counter.object2LongEntrySet().stream().sorted((e, f) -> Long.compare(f.getLongValue(), e.getLongValue())).map(e ->
{
Item item = e.getKey();
MutableComponent itemName = Component.translatable(item.getDescriptionId());
Style itemStyle = itemName.getStyle();
TextColor color = guessColor(item, server.registryAccess());
TextColor color = guessColor(item, server.overworld());
itemName.setStyle((color != null) ? itemStyle.withColor(color) : itemStyle.withItalic(true));
long count = e.getLongValue();
return Messenger.c("g - ", itemName,
"g : ","wb "+count,"g , ",
"g : ", "wb " + count, "g , ",
String.format("wb %.1f", count * (20.0 * 60.0 * 60.0) / ticks), "w /h"
);
}).collect(Collectors.toList()));
@@ -231,13 +240,25 @@ public class HopperCounter
*/
public static int appropriateColor(int color)
{
if (color == 0) return MapColor.SNOW.col;
if (color == 0)
{
return MapColor.SNOW.col;
}
int r = (color >> 16 & 255);
int g = (color >> 8 & 255);
int b = (color & 255);
if (r < 70) r = 70;
if (g < 70) g = 70;
if (b < 70) b = 70;
if (r < 70)
{
r = 70;
}
if (g < 70)
{
g = 70;
}
if (b < 70)
{
b = 70;
}
return (r << 16) + (g << 8) + b;
}
@@ -271,65 +292,65 @@ public class HopperCounter
entry(Items.ROSE_BUSH, Blocks.RED_WOOL),
entry(Items.PEONY, Blocks.PINK_WOOL),
entry(Items.CARROT, Blocks.ORANGE_WOOL),
entry(Items.APPLE,Blocks.RED_WOOL),
entry(Items.WHEAT,Blocks.HAY_BLOCK),
entry(Items.APPLE, Blocks.RED_WOOL),
entry(Items.WHEAT, Blocks.HAY_BLOCK),
entry(Items.PORKCHOP, Blocks.PINK_WOOL),
entry(Items.RABBIT,Blocks.PINK_WOOL),
entry(Items.CHICKEN,Blocks.WHITE_TERRACOTTA),
entry(Items.BEEF,Blocks.NETHERRACK),
entry(Items.ENCHANTED_GOLDEN_APPLE,Blocks.GOLD_BLOCK),
entry(Items.COD,Blocks.WHITE_TERRACOTTA),
entry(Items.SALMON,Blocks.ACACIA_PLANKS),
entry(Items.ROTTEN_FLESH,Blocks.BROWN_WOOL),
entry(Items.PUFFERFISH,Blocks.YELLOW_TERRACOTTA),
entry(Items.TROPICAL_FISH,Blocks.ORANGE_WOOL),
entry(Items.POTATO,Blocks.WHITE_TERRACOTTA),
entry(Items.RABBIT, Blocks.PINK_WOOL),
entry(Items.CHICKEN, Blocks.WHITE_TERRACOTTA),
entry(Items.BEEF, Blocks.NETHERRACK),
entry(Items.ENCHANTED_GOLDEN_APPLE, Blocks.GOLD_BLOCK),
entry(Items.COD, Blocks.WHITE_TERRACOTTA),
entry(Items.SALMON, Blocks.ACACIA_PLANKS),
entry(Items.ROTTEN_FLESH, Blocks.BROWN_WOOL),
entry(Items.PUFFERFISH, Blocks.YELLOW_TERRACOTTA),
entry(Items.TROPICAL_FISH, Blocks.ORANGE_WOOL),
entry(Items.POTATO, Blocks.WHITE_TERRACOTTA),
entry(Items.MUTTON, Blocks.RED_WOOL),
entry(Items.BEETROOT,Blocks.NETHERRACK),
entry(Items.MELON_SLICE,Blocks.MELON),
entry(Items.POISONOUS_POTATO,Blocks.SLIME_BLOCK),
entry(Items.SPIDER_EYE,Blocks.NETHERRACK),
entry(Items.GUNPOWDER,Blocks.GRAY_WOOL),
entry(Items.TURTLE_SCUTE,Blocks.LIME_WOOL),
entry(Items.ARMADILLO_SCUTE,Blocks.ANCIENT_DEBRIS),
entry(Items.FEATHER,Blocks.WHITE_WOOL),
entry(Items.FLINT,Blocks.BLACK_WOOL),
entry(Items.LEATHER,Blocks.SPRUCE_PLANKS),
entry(Items.GLOWSTONE_DUST,Blocks.GLOWSTONE),
entry(Items.PAPER,Blocks.WHITE_WOOL),
entry(Items.BRICK,Blocks.BRICKS),
entry(Items.INK_SAC,Blocks.BLACK_WOOL),
entry(Items.SNOWBALL,Blocks.SNOW_BLOCK),
entry(Items.WATER_BUCKET,Blocks.WATER),
entry(Items.LAVA_BUCKET,Blocks.LAVA),
entry(Items.MILK_BUCKET,Blocks.WHITE_WOOL),
entry(Items.BEETROOT, Blocks.NETHERRACK),
entry(Items.MELON_SLICE, Blocks.MELON),
entry(Items.POISONOUS_POTATO, Blocks.SLIME_BLOCK),
entry(Items.SPIDER_EYE, Blocks.NETHERRACK),
entry(Items.GUNPOWDER, Blocks.GRAY_WOOL),
entry(Items.TURTLE_SCUTE, Blocks.LIME_WOOL),
entry(Items.ARMADILLO_SCUTE, Blocks.ANCIENT_DEBRIS),
entry(Items.FEATHER, Blocks.WHITE_WOOL),
entry(Items.FLINT, Blocks.BLACK_WOOL),
entry(Items.LEATHER, Blocks.SPRUCE_PLANKS),
entry(Items.GLOWSTONE_DUST, Blocks.GLOWSTONE),
entry(Items.PAPER, Blocks.WHITE_WOOL),
entry(Items.BRICK, Blocks.BRICKS),
entry(Items.INK_SAC, Blocks.BLACK_WOOL),
entry(Items.SNOWBALL, Blocks.SNOW_BLOCK),
entry(Items.WATER_BUCKET, Blocks.WATER),
entry(Items.LAVA_BUCKET, Blocks.LAVA),
entry(Items.MILK_BUCKET, Blocks.WHITE_WOOL),
entry(Items.CLAY_BALL, Blocks.CLAY),
entry(Items.COCOA_BEANS,Blocks.COCOA),
entry(Items.BONE,Blocks.BONE_BLOCK),
entry(Items.COD_BUCKET,Blocks.BROWN_TERRACOTTA),
entry(Items.PUFFERFISH_BUCKET,Blocks.YELLOW_TERRACOTTA),
entry(Items.SALMON_BUCKET,Blocks.PINK_TERRACOTTA),
entry(Items.TROPICAL_FISH_BUCKET,Blocks.ORANGE_TERRACOTTA),
entry(Items.SUGAR,Blocks.WHITE_WOOL),
entry(Items.BLAZE_POWDER,Blocks.GOLD_BLOCK),
entry(Items.ENDER_PEARL,Blocks.WARPED_PLANKS),
entry(Items.NETHER_STAR,Blocks.DIAMOND_BLOCK),
entry(Items.PRISMARINE_CRYSTALS,Blocks.SEA_LANTERN),
entry(Items.PRISMARINE_SHARD,Blocks.PRISMARINE),
entry(Items.RABBIT_HIDE,Blocks.OAK_PLANKS),
entry(Items.CHORUS_FRUIT,Blocks.PURPUR_BLOCK),
entry(Items.SHULKER_SHELL,Blocks.SHULKER_BOX),
entry(Items.NAUTILUS_SHELL,Blocks.BONE_BLOCK),
entry(Items.HEART_OF_THE_SEA,Blocks.CONDUIT),
entry(Items.HONEYCOMB,Blocks.HONEYCOMB_BLOCK),
entry(Items.NAME_TAG,Blocks.BONE_BLOCK),
entry(Items.TOTEM_OF_UNDYING,Blocks.YELLOW_TERRACOTTA),
entry(Items.TRIDENT,Blocks.PRISMARINE),
entry(Items.GHAST_TEAR,Blocks.WHITE_WOOL),
entry(Items.PHANTOM_MEMBRANE,Blocks.BONE_BLOCK),
entry(Items.EGG,Blocks.BONE_BLOCK),
entry(Items.COCOA_BEANS, Blocks.COCOA),
entry(Items.BONE, Blocks.BONE_BLOCK),
entry(Items.COD_BUCKET, Blocks.BROWN_TERRACOTTA),
entry(Items.PUFFERFISH_BUCKET, Blocks.YELLOW_TERRACOTTA),
entry(Items.SALMON_BUCKET, Blocks.PINK_TERRACOTTA),
entry(Items.TROPICAL_FISH_BUCKET, Blocks.ORANGE_TERRACOTTA),
entry(Items.SUGAR, Blocks.WHITE_WOOL),
entry(Items.BLAZE_POWDER, Blocks.GOLD_BLOCK),
entry(Items.ENDER_PEARL, Blocks.WARPED_PLANKS),
entry(Items.NETHER_STAR, Blocks.DIAMOND_BLOCK),
entry(Items.PRISMARINE_CRYSTALS, Blocks.SEA_LANTERN),
entry(Items.PRISMARINE_SHARD, Blocks.PRISMARINE),
entry(Items.RABBIT_HIDE, Blocks.OAK_PLANKS),
entry(Items.CHORUS_FRUIT, Blocks.PURPUR_BLOCK),
entry(Items.SHULKER_SHELL, Blocks.SHULKER_BOX),
entry(Items.NAUTILUS_SHELL, Blocks.BONE_BLOCK),
entry(Items.HEART_OF_THE_SEA, Blocks.CONDUIT),
entry(Items.HONEYCOMB, Blocks.HONEYCOMB_BLOCK),
entry(Items.NAME_TAG, Blocks.BONE_BLOCK),
entry(Items.TOTEM_OF_UNDYING, Blocks.YELLOW_TERRACOTTA),
entry(Items.TRIDENT, Blocks.PRISMARINE),
entry(Items.GHAST_TEAR, Blocks.WHITE_WOOL),
entry(Items.PHANTOM_MEMBRANE, Blocks.BONE_BLOCK),
entry(Items.EGG, Blocks.BONE_BLOCK),
//entry(Items.,Blocks.),
entry(Items.COPPER_INGOT,Blocks.COPPER_BLOCK),
entry(Items.COPPER_INGOT, Blocks.COPPER_BLOCK),
entry(Items.AMETHYST_SHARD, Blocks.AMETHYST_BLOCK));
/**
@@ -337,8 +358,14 @@ public class HopperCounter
*/
public static TextColor fromItem(Item item, RegistryAccess registryAccess)
{
if (DEFAULTS.containsKey(item)) return TextColor.fromRgb(appropriateColor(DEFAULTS.get(item).defaultMapColor().col));
if (item instanceof DyeItem dye) return TextColor.fromRgb(appropriateColor(dye.getDyeColor().getMapColor().col));
if (DEFAULTS.containsKey(item))
{
return TextColor.fromRgb(appropriateColor(DEFAULTS.get(item).defaultMapColor().col));
}
if (item instanceof DyeItem dye)
{
return TextColor.fromRgb(appropriateColor(dye.getDyeColor().getMapColor().col));
}
Block block = null;
final Registry<Item> itemRegistry = registryAccess.lookupOrThrow(Registries.ITEM);
final Registry<Block> blockRegistry = registryAccess.lookupOrThrow(Registries.BLOCK);
@@ -353,9 +380,15 @@ public class HopperCounter
}
if (block != null)
{
if (block instanceof AbstractBannerBlock) return TextColor.fromRgb(appropriateColor(((AbstractBannerBlock) block).getColor().getMapColor().col));
if (block instanceof BeaconBeamBlock) return TextColor.fromRgb(appropriateColor( ((BeaconBeamBlock) block).getColor().getMapColor().col));
return TextColor.fromRgb(appropriateColor( block.defaultMapColor().col));
if (block instanceof AbstractBannerBlock)
{
return TextColor.fromRgb(appropriateColor(((AbstractBannerBlock) block).getColor().getMapColor().col));
}
if (block instanceof BeaconBeamBlock)
{
return TextColor.fromRgb(appropriateColor(((BeaconBeamBlock) block).getColor().getMapColor().col));
}
return TextColor.fromRgb(appropriateColor(block.defaultMapColor().col));
}
return null;
}
@@ -364,35 +397,48 @@ public class HopperCounter
* Guesses the item's colour from the item itself. It first calls {@link HopperCounter#fromItem} to see if it has a
* valid colour there, if not just makes a guess, and if that fails just returns null
*/
public static TextColor guessColor(Item item, RegistryAccess registryAccess)
public static TextColor guessColor(Item item, Level level)
{
RegistryAccess registryAccess = level.registryAccess();
TextColor direct = fromItem(item, registryAccess);
if (direct != null) return direct;
if (CarpetServer.minecraft_server == null) return WHITE;
if (direct != null)
{
return direct;
}
if (CarpetServer.minecraft_server == null)
{
return WHITE;
}
ResourceLocation id = registryAccess.lookupOrThrow(Registries.ITEM).getKey(item);
for (RecipeType<?> type: registryAccess.lookupOrThrow(Registries.RECIPE_TYPE))
if (id == null)
{
for (Recipe<?> r: ((RecipeManagerInterface) CarpetServer.minecraft_server.getRecipeManager()).getAllMatching(type, id, registryAccess))
return null;
}
for (Recipe<?> r : RecipeHelper.getRecipesForOutput(CarpetServer.minecraft_server.getRecipeManager(), id, level))
{
for (Ingredient ingredient : r.placementInfo().ingredients())
{
for (Optional<PlacementInfo.SlotInfo> ingredient: r.placementInfo().slotInfo())
for (Holder<Item> stack : ingredient.items())
{
for(ItemStack stack : ingredient.get().possibleItems())
TextColor cand = fromItem(stack.value(), registryAccess);
if (cand != null)
{
TextColor cand = fromItem(stack.getItem(), registryAccess);
if (cand != null)
return cand;
return cand;
}
}
}
}
return null;
}
/**
* Returns the hopper counter for the given color
*/
public static HopperCounter getCounter(DyeColor color) {
public static HopperCounter getCounter(DyeColor color)
{
return COUNTERS.get(color);
}
@@ -417,6 +463,6 @@ public class HopperCounter
*/
public long getTotalItems()
{
return counter.isEmpty()?0:counter.values().longStream().sum();
return counter.isEmpty() ? 0 : counter.values().longStream().sum();
}
}