Skip to content

Commit

Permalink
fixes (#490)
Browse files Browse the repository at this point in the history
* fix: SoundEntryProvider uses the wrong modid if you don't call it from GT.

* fix: material tooltips on non-GT items.

* fix: add ChemicalHelper to KJS bindings

* fix: stone can be crafted to dust in crafting table, other issues like that

* fix: tools only have "digger" enchantments

* fix #488

* try to fix #458

* chore: changelog, version bump

* revert fix. didn't fix.

* feat: fluid tooltips (untested this laptop is too slow)

* feat: fluid tooltips (untested this laptop is too slow)

* final fixes to stuff
stop adding tooltips to other mods' items as a temp fix to it being _really_ slow to compute

* todo comment

Co-authored-by: Mikerooni <[email protected]>

* Update common/src/main/java/com/gregtechceu/gtceu/api/data/chemical/ChemicalHelper.java

Co-authored-by: Mikerooni <[email protected]>

---------

Co-authored-by: Mikerooni <[email protected]>
  • Loading branch information
screret and mikerooni authored Oct 24, 2023
1 parent c22768c commit df3af09
Show file tree
Hide file tree
Showing 23 changed files with 293 additions and 25 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ChangeLog

* fix feature order cycle on fabric
* make some API methods more addon dev friendly
* add tags for all purified/refined ores
* fix tools only accepting digger enchantments
* fix stone, netherrack, hay being craftable to 9 dust in a crafting table
* fix machines sometimes crashing when auto-output is toggled rapidly, resulting in it being enabled but no output side being set
* fix SoundEntryProvider always placing the JSON in /assets/gtceu/sounds.json
* add material tooltips for non-GT items
* add KJS binding for ChemicalHelper
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource

@Override
public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) {
if (!TagPrefix.ORES.containsKey(this.tagPrefix) || (tagPrefix == TagPrefix.oreSand || tagPrefix == TagPrefix.oreRedSand) || !ConfigHolder.INSTANCE.worldgen.sandOresFall) return;
if (!TagPrefix.ORES.containsKey(this.tagPrefix) || !TagPrefix.ORES.get(tagPrefix).isSand() || !ConfigHolder.INSTANCE.worldgen.sandOresFall) return;
if (random.nextInt(16) == 0 && FallingBlock.isFree(level.getBlockState(pos.below()))) {
double d = (double)pos.getX() + random.nextDouble();
double e = (double)pos.getY() - 0.05;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package com.gregtechceu.gtceu.api.data.chemical;

import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.FluidProperty;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.ItemMaterialInfo;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialStack;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.UnificationEntry;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
import com.gregtechceu.gtceu.api.data.tag.TagUtil;
import com.gregtechceu.gtceu.api.fluids.store.FluidStorageKey;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.common.data.GTBlocks;
import com.gregtechceu.gtceu.common.data.GTItems;
import com.gregtechceu.gtceu.data.tags.TagsHandler;
import com.lowdragmc.lowdraglib.Platform;
import com.tterrag.registrate.util.entry.BlockEntry;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -39,12 +47,16 @@ public class ChemicalHelper {
/** Used for custom material data for items that do not fall into the normal "prefix, material" pair */
public static final Map<ItemLike, ItemMaterialInfo> ITEM_MATERIAL_INFO = new Object2ObjectLinkedOpenHashMap<>();
/** Mapping of an item to a "prefix, material" pair */
public static final Map<ItemLike, UnificationEntry> ITEM_UNIFICATION_ENTRY = new HashMap<>();
public static final Map<ItemLike, UnificationEntry> ITEM_UNIFICATION_ENTRY = new Object2ObjectLinkedOpenHashMap<>();
/** Mapping of a tag to a "prefix, material" pair */
public static final Map<TagKey<Item>, UnificationEntry> TAG_UNIFICATION_ENTRY = new Object2ObjectLinkedOpenHashMap<>();
/** Mapping of a fluid to a material */
public static final Map<Fluid, Material> FLUID_MATERIAL = new Object2ObjectLinkedOpenHashMap<>();
/** Mapping of all items that represent a "prefix, material" pair */
public static final Map<UnificationEntry, ArrayList<ItemLike>> UNIFICATION_ENTRY_ITEM = new Object2ObjectLinkedOpenHashMap<>();
public static final Map<UnificationEntry, ArrayList<Block>> UNIFICATION_ENTRY_BLOCK = new Object2ObjectLinkedOpenHashMap<>();
/** Mapping of stone type blockState to "prefix, material" */
public static final Map<Supplier<BlockState>, TagPrefix> ORES_INVERSE = new HashMap<>();
public static final Map<Supplier<BlockState>, TagPrefix> ORES_INVERSE = new Object2ObjectLinkedOpenHashMap<>();

public static void registerMaterialInfo(ItemLike item, ItemMaterialInfo materialInfo) {
ITEM_MATERIAL_INFO.put(item, materialInfo);
Expand Down Expand Up @@ -106,6 +118,27 @@ public static MaterialStack getMaterial(ItemLike itemLike) {
return info == null ? null : info.getMaterial().copy();
}

@Nullable
public static Material getMaterial(Fluid fluid) {
return FLUID_MATERIAL.computeIfAbsent(fluid, f -> {
for (Material material : GTRegistries.MATERIALS) {
if (material.hasProperty(PropertyKey.FLUID)) {
FluidProperty property = material.getProperty(PropertyKey.FLUID);
for (FluidStorageKey key : FluidStorageKey.allKeys()) {
Fluid stored = property.getStorage().get(key);
TagKey<Fluid> tag = TagUtil.createFluidTag(BuiltInRegistries.FLUID.getKey(stored).getPath());
if (!Platform.isForge() && tag.location().equals(new ResourceLocation("water")) && !stored.isSame(Fluids.WATER)) continue;
if (!Platform.isForge() && tag.location().equals(new ResourceLocation("lava")) && !stored.isSame(Fluids.LAVA)) continue;
if (f == stored || f.is(tag)) {
return material;
}
}
}
}
return null;
});
}

@Nullable
public static TagPrefix getPrefix(ItemLike itemLike) {
if (itemLike == null) return null;
Expand Down Expand Up @@ -170,6 +203,29 @@ public static UnificationEntry getUnificationEntry(ItemLike item) {
return ITEM_UNIFICATION_ENTRY.get(item);
}

// TODO optimize this so it can be used in tooltips/etc.
public static UnificationEntry getUnificationEntry(TagKey<Item> tag) {
return TAG_UNIFICATION_ENTRY.computeIfAbsent(tag, tagKey -> {
for (TagPrefix prefix : TagPrefix.values()) {
for (Material material : GTRegistries.MATERIALS) {
if (Arrays.stream(prefix.getItemTags(material)).anyMatch(tagKey1 -> tagKey1.location().equals(tagKey.location()))) {
return new UnificationEntry(prefix, material);
}
}
}
return new UnificationEntry.EmptyMapMarkerEntry();
});
}

@Nullable
public static UnificationEntry getOrComputeUnificationEntry(ItemLike item) {
return ITEM_UNIFICATION_ENTRY.computeIfAbsent(item, itemLike -> {
Holder<Item> holder = BuiltInRegistries.ITEM.wrapAsHolder(itemLike.asItem());
return holder.tags().map(ChemicalHelper::getUnificationEntry).filter(Objects::nonNull)
.filter(entry -> !(entry instanceof UnificationEntry.EmptyMapMarkerEntry)).findFirst().orElse(null);
});
}

public static List<ItemLike> getItems(UnificationEntry unificationEntry) {
return UNIFICATION_ENTRY_ITEM.computeIfAbsent(unificationEntry, entry -> {
var items = new ArrayList<ItemLike>();
Expand Down Expand Up @@ -261,6 +317,7 @@ public static void reinitializeUnification() {
ChemicalHelper.UNIFICATION_ENTRY_ITEM.clear();
ChemicalHelper.UNIFICATION_ENTRY_BLOCK.clear();
ChemicalHelper.ITEM_UNIFICATION_ENTRY.clear();
ChemicalHelper.FLUID_MATERIAL.clear();

// Load new data
TagsHandler.initExtraUnificationEntries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,25 @@ public String toString() {
return tagPrefix.name + (material != null ? material.toCamelCaseString() : "");
}

public static class EmptyMapMarkerEntry extends UnificationEntry {

public EmptyMapMarkerEntry() {
super(null);
}

@Override
public boolean equals(Object o) {
return this == o;
}

@Override
public int hashCode() {
return 0;
}

@Override
public String toString() {
return "EMPTY UNIFICATION ENTRY";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static TagPrefix get(String name) {
return PREFIXES.get(name);
}

public static final TagPrefix ore = oreTagPrefix("stone")
public static final TagPrefix ore = oreTagPrefix("ore")
.langValue("%s Ore")
.materialIconType(MaterialIconType.ore)
.miningToolTag(BlockTags.MINEABLE_WITH_PICKAXE)
Expand Down Expand Up @@ -689,6 +689,14 @@ public static TagPrefix get(String name) {
.miningToolTag(BlockTags.MINEABLE_WITH_PICKAXE)
.unificationEnabled(true);

// Prefix to determine which kind of Rock this is.
public static final TagPrefix stone = new TagPrefix("stone")
.defaultTagPath(FORGE, "%s")
.defaultTagPath(FABRIC, "%s")
.langValue("%s")
.miningToolTag(BlockTags.MINEABLE_WITH_PICKAXE)
.unificationEnabled(true);

public static final TagPrefix frameGt = new TagPrefix("frameGt")
.defaultTagPath(FORGE, "frames/%s")
.defaultTagPath(FABRIC, "%s_frames")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void scan(String[][][] storage, LevelChunk chunk) {
if (state.is(oreTag)) {
var itemName = BLOCK_CACHE.computeIfAbsent(state, blockState -> {
var name = BuiltInRegistries.BLOCK.getKey(blockState.getBlock()).toString();
var entry = ChemicalHelper.getUnificationEntry(blockState.getBlock());
var entry = ChemicalHelper.getOrComputeUnificationEntry(blockState.getBlock());
if (entry != null && entry.material != null) {
name = "material_" + entry.material.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public void exportToNearby(Direction... facings) {
var level = getMachine().getLevel();
var pos = getMachine().getPos();
for (Direction facing : facings) {
if (facing == null) continue; // TODO find actual fix
ItemTransferHelper.exportToTarget(this, Integer.MAX_VALUE, f -> true, level, pos.relative(facing), facing.getOpposite());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SoundEntryProvider(PackOutput output, String modId) {

@Override
public CompletableFuture<?> run(CachedOutput cache) {
return generate(output.getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(GTCEu.MOD_ID), cache);
return generate(output.getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(modId), cache);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.material.Fluid;

import java.util.List;

Expand All @@ -37,7 +38,7 @@ public static void appendTooltips(ItemStack stack, TooltipFlag flag, List<Compon
}

// Formula
var unificationEntry = ChemicalHelper.getUnificationEntry(stack.getItem());
var unificationEntry = ChemicalHelper.getUnificationEntry(stack.getItem()); // TODO optimize getOrComputeUnificationEntry so we can use that
if (unificationEntry != null && unificationEntry.material != null) {
if (unificationEntry.material.getChemicalFormula() != null && !unificationEntry.material.getChemicalFormula().isEmpty())
tooltips.add(1, Component.literal(unificationEntry.material.getChemicalFormula()).withStyle(ChatFormatting.YELLOW));
Expand All @@ -57,4 +58,12 @@ public static void appendTooltips(ItemStack stack, TooltipFlag flag, List<Compon
}
}
}

public static void appendFluidTooltips(Fluid fluid, List<Component> tooltips, TooltipFlag flag) {
var material = ChemicalHelper.getMaterial(fluid);
if (material != null) {
if (material.getChemicalFormula() != null && !material.getChemicalFormula().isEmpty())
tooltips.add(1, Component.literal(material.getChemicalFormula()).withStyle(ChatFormatting.YELLOW));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,7 @@ public static void init() {
block.setIgnored(Coal, Blocks.COAL_BLOCK);
block.setIgnored(Amethyst, Blocks.AMETHYST_BLOCK);
block.setIgnored(Glass, Blocks.GLASS);
block.setIgnored(Marble);
block.setIgnored(Granite, Blocks.GRANITE);
block.setIgnored(GraniteRed);
block.setIgnored(Andesite, Blocks.ANDESITE);
block.setIgnored(Diorite, Blocks.DIORITE);
block.setIgnored(Stone, Blocks.STONE);
block.setIgnored(Glowstone, Blocks.GLOWSTONE);
block.setIgnored(Endstone, Blocks.END_STONE);
block.setIgnored(Wheat, Blocks.HAY_BLOCK);
block.setIgnored(Oilsands);
block.setIgnored(Wood);
block.setIgnored(TreatedWood);
Expand All @@ -207,13 +199,24 @@ public static void init() {
block.setIgnored(Bone, Blocks.BONE_BLOCK);
block.setIgnored(NetherQuartz, Blocks.QUARTZ_BLOCK);
block.setIgnored(Ice, Blocks.ICE);
block.setIgnored(Netherrack, Blocks.NETHERRACK);
block.setIgnored(Concrete, Blocks.WHITE_CONCRETE, Blocks.ORANGE_CONCRETE, Blocks.MAGENTA_CONCRETE, Blocks.LIGHT_BLUE_CONCRETE, Blocks.YELLOW_CONCRETE, Blocks.LIME_CONCRETE,
Blocks.PINK_CONCRETE, Blocks.GRAY_CONCRETE, Blocks.LIGHT_GRAY_CONCRETE, Blocks.CYAN_CONCRETE, Blocks.PURPLE_CONCRETE, Blocks.BLUE_CONCRETE,
Blocks.BROWN_CONCRETE, Blocks.GREEN_CONCRETE, Blocks.RED_CONCRETE, Blocks.BLACK_CONCRETE);
block.setIgnored(Blaze);
block.setIgnored(Lapotron);
block.setIgnored(Obsidian, Blocks.OBSIDIAN);

stone.setIgnored(Marble);
stone.setIgnored(Granite, Blocks.GRANITE);
stone.setIgnored(Granite, Blocks.POLISHED_GRANITE);
stone.setIgnored(GraniteRed);
stone.setIgnored(Andesite, Blocks.ANDESITE);
stone.setIgnored(Andesite, Blocks.POLISHED_ANDESITE);
stone.setIgnored(Diorite, Blocks.DIORITE);
stone.setIgnored(Diorite, Blocks.POLISHED_DIORITE);
stone.setIgnored(Stone, Blocks.STONE);
stone.setIgnored(Netherrack, Blocks.NETHERRACK);
stone.setIgnored(Obsidian, Blocks.OBSIDIAN);
stone.setIgnored(Endstone, Blocks.END_STONE);

crushed.addSecondaryMaterial(new MaterialStack(Stone, dust.materialAmount()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return MixinPluginShared.isClassFound("dev.latvian.mods.kubejs.KubeJSPlugin");
} else if (mixinClassName.contains("com.gregtechceu.gtceu.core.mixins.create")) {
return MixinPluginShared.isClassFound("com.simibubi.create.compat.Mods");
} else if (mixinClassName.contains("com.gregtechceu.gtceu.core.mixins.rei")) {
return MixinPluginShared.isClassFound("me.shedaniel.rei.api.common.plugins.REIPlugin");
} else if (mixinClassName.contains("com.gregtechceu.gtceu.fabric.core.mixins.kjs")) {
return MixinPluginShared.isClassFound("dev.latvian.mods.kubejs.fabric.KubeJSFabric");
} else if (mixinClassName.contains("com.gregtechceu.gtceu.forge.core.mixins.kjs")) {
return MixinPluginShared.isClassFound("dev.latvian.mods.kubejs.forge.KubeJSForge");
} else if (mixinClassName.contains("com.gregtechceu.gtceu.core.mixins.top")) {
return MixinPluginShared.isClassFound("mcjty.theoneprobe.api.ITheOneProbe");
} else if (mixinClassName.contains("com.gregtechceu.gtceu.forge.core.mixins.jei")) {
return MixinPluginShared.isClassFound("mezz.jei.api.IModPlugin");
}else if (mixinClassName.contains("com.gregtechceu.gtceu.forge.core.mixins.emi")) {
return MixinPluginShared.isClassFound("dev.emi.emi.api.EmiPlugin");
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.gregtechceu.gtceu.core.mixins;

import com.gregtechceu.gtceu.api.item.GTToolItem;
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(targets = {"net.minecraft.world.item.enchantment.EnchantmentCategory$6",
"net.minecraft.world.item.enchantment.EnchantmentCategory$7",
"net.minecraft.world.item.enchantment.EnchantmentCategory$9",
"net.minecraft.world.item.enchantment.EnchantmentCategory$11",
"net.minecraft.world.item.enchantment.EnchantmentCategory$13"})
public class MixinEnchantmentCategory {

@Inject(method = "canEnchant(Lnet/minecraft/world/item/Item;)Z", at = @At("RETURN"), cancellable = true)
private void gtceu$canEnchantTool(Item item, CallbackInfoReturnable<Boolean> cir) {
if (item instanceof GTToolItem gtItem) {
cir.setReturnValue(switch ((EnchantmentCategory)(Object)this) {
case WEAPON -> gtItem.getToolType() == GTToolType.SWORD ||
gtItem.getToolType() == GTToolType.BUTCHERY_KNIFE ||
gtItem.getToolType() == GTToolType.KNIFE;
case DIGGER -> gtItem.getToolType() == GTToolType.AXE ||
gtItem.getToolType() == GTToolType.PICKAXE ||
gtItem.getToolType() == GTToolType.SHOVEL ||
gtItem.getToolType() == GTToolType.HOE ||
gtItem.getToolType() == GTToolType.MINING_HAMMER ||
gtItem.getToolType() == GTToolType.SAW ||
gtItem.getToolType() == GTToolType.HARD_HAMMER ||
gtItem.getToolType() == GTToolType.SOFT_MALLET ||
gtItem.getToolType() == GTToolType.WRENCH ||
gtItem.getToolType() == GTToolType.FILE ||
gtItem.getToolType() == GTToolType.CROWBAR ||
gtItem.getToolType() == GTToolType.SCREWDRIVER ||
gtItem.getToolType() == GTToolType.SCYTHE ||
gtItem.getToolType() == GTToolType.PLUNGER;
default -> cir.getReturnValueZ();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public class TagLoaderMixin<T> implements IGTTagLoader<T> {
for (FluidStorageKey key : FluidStorageKey.allKeys()) {
Fluid fluid = property.getStorage().get(key);
if (fluid != null) {
ChemicalHelper.FLUID_MATERIAL.put(fluid, material);

ResourceLocation fluidId = BuiltInRegistries.FLUID.getKey(fluid);
tagMap.computeIfAbsent(TagUtil.createFluidTag(fluidId.getPath()).location(), path -> new ArrayList<>())
.add(new TagLoader.EntryWithSource(TagEntry.element(fluidId), GTValues.CUSTOM_TAG_SOURCE));
Expand Down
Loading

0 comments on commit df3af09

Please sign in to comment.