Fix some items not rendering in inventories

This commit is contained in:
embeddedt 2023-04-29 14:06:53 -04:00
parent dc56d51f11
commit 5ac369acb8
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 100 additions and 12 deletions

View File

@ -6,18 +6,26 @@ import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraftforge.client.ItemModelMesherForge;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IRegistryDelegate;
import org.embeddedt.modernfix.dynamicresources.ModelLocationCache;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Mixin(ItemModelMesherForge.class)
public abstract class ItemModelShaperMixin extends ItemModelShaper {
@Shadow @Final private Map<IRegistryDelegate<Item>, ModelResourceLocation> locations;
@Shadow @Final @Mutable private Map<IRegistryDelegate<Item>, ModelResourceLocation> locations;
private Map<IRegistryDelegate<Item>, ModelResourceLocation> overrideLocations;
public ItemModelShaperMixin(ModelManager arg) {
super(arg);
@ -25,6 +33,86 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper {
private static final ModelResourceLocation SENTINEL = new ModelResourceLocation("modernfix", "sentinel");
@Inject(method = "<init>", at = @At("RETURN"))
private void replaceLocationMap(CallbackInfo ci) {
overrideLocations = new HashMap<>();
// need to replace this map because mods query locations through it
locations = new Map<IRegistryDelegate<Item>, ModelResourceLocation>() {
@Override
public int size() {
return ForgeRegistries.ITEMS.getValues().size();
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean containsKey(Object key) {
return true;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public ModelResourceLocation get(Object key) {
return getLocation(((IRegistryDelegate<Item>)key).get());
}
@Nullable
@Override
public ModelResourceLocation put(IRegistryDelegate<Item> key, ModelResourceLocation value) {
throw new UnsupportedOperationException();
}
@Override
public ModelResourceLocation remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(@NotNull Map<? extends IRegistryDelegate<Item>, ? extends ModelResourceLocation> m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public Set<IRegistryDelegate<Item>> keySet() {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public Collection<ModelResourceLocation> values() {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public Set<Entry<IRegistryDelegate<Item>, ModelResourceLocation>> entrySet() {
throw new UnsupportedOperationException();
}
};
}
private ModelResourceLocation getLocation(Item item) {
ModelResourceLocation map = overrideLocations.getOrDefault(item.delegate, SENTINEL);
if(map == SENTINEL) {
/* generate the appropriate location from our cache */
map = ModelLocationCache.get(item);
}
return map;
}
/**
* @reason Get the stored location for that item and meta, and get the model
* from that location from the model manager.
@ -32,11 +120,7 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper {
@Overwrite
@Override
public BakedModel getItemModel(Item item) {
ModelResourceLocation map = locations.getOrDefault(item.delegate, SENTINEL);
if(map == SENTINEL) {
/* generate the appropriate location from our cache */
map = ModelLocationCache.get(item);
}
ModelResourceLocation map = getLocation(item);
return map == null ? null : getModelManager().getModel(map);
}
@ -47,7 +131,7 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper {
@Overwrite
@Override
public void register(Item item, ModelResourceLocation location) {
locations.put(item.delegate, location);
overrideLocations.put(item.delegate, location);
}
/**

View File

@ -515,8 +515,10 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
}
@Redirect(method = "loadModel", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/StateDefinition;getPossibleStates()Lcom/google/common/collect/ImmutableList;"))
private ImmutableList<BlockState> loadOnlyRelevantBlockState(StateDefinition<Block, BlockState> stateDefinition, ResourceLocation location) {
Set<Property<?>> fixedProperties = new HashSet<>();
ModelResourceLocation mrl = (ModelResourceLocation)location;
if(Objects.equals(mrl.getVariant(), "inventory"))
return ImmutableList.of();
Set<Property<?>> fixedProperties = new HashSet<>();
BlockState fixedState = stateDefinition.any();
for(String s : COMMA_SPLITTER.split(mrl.getVariant())) {
Iterator<String> iterator = EQUAL_SPLITTER.split(s).iterator();
@ -574,6 +576,8 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
LOGGER.info("Baking {}", arg);
UnbakedModel iunbakedmodel = this.getModel(arg);
iunbakedmodel.getMaterials(this::getModel, new HashSet<>());
if(iunbakedmodel == missingModel && debugDynamicModelLoading)
LOGGER.warn("Model {} not present", arg);
BakedModel ibakedmodel = null;
if (iunbakedmodel instanceof BlockModel) {
BlockModel blockmodel = (BlockModel)iunbakedmodel;