-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dd9033
commit 601fac7
Showing
9 changed files
with
287 additions
and
48 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/main/java/com/nomiceu/nomilabs/mixin/ForgeRegistryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.nomiceu.nomilabs.mixin; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.event.RegistryEvent; | ||
import net.minecraftforge.registries.ForgeRegistry; | ||
import net.minecraftforge.registries.IForgeRegistryEntry; | ||
import net.minecraftforge.registries.IForgeRegistryInternal; | ||
import net.minecraftforge.registries.IForgeRegistryModifiable; | ||
|
||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import com.nomiceu.nomilabs.NomiLabs; | ||
import com.nomiceu.nomilabs.mixinhelper.RemappableForgeRegistry; | ||
import com.nomiceu.nomilabs.mixinhelper.RemappableSnapshot; | ||
|
||
/** | ||
* This mixin saves a new map in Forge Registries: Remapped.<br> | ||
* This is a map of remapped block ids to their new resource location, as specified by a remapper.<br> | ||
* Note that this is only applied when the new resource location cannot be registered at the old id, as in that case, no | ||
* saving of ids is required, as id loading will already load the new resource location.<br> | ||
* <p> | ||
* This mixin also fixes blocked lists not syncing between Forge Registries. | ||
*/ | ||
@Mixin(value = ForgeRegistry.class, remap = false) | ||
public abstract class ForgeRegistryMixin<V extends IForgeRegistryEntry<V>> | ||
implements IForgeRegistryInternal<V>, IForgeRegistryModifiable<V>, | ||
RemappableForgeRegistry { | ||
|
||
@Shadow | ||
abstract void block(int id); | ||
|
||
@Shadow | ||
@Final | ||
private Set<Integer> blocked; | ||
|
||
@Unique | ||
private final Map<Integer, ResourceLocation> remapped = Maps.newHashMap(); | ||
|
||
@Inject(method = "processMissingEvent", | ||
at = @At(value = "INVOKE", | ||
target = "Lnet/minecraftforge/registries/ForgeRegistry;addAlias(Lnet/minecraft/util/ResourceLocation;Lnet/minecraft/util/ResourceLocation;)V"), | ||
require = 1, | ||
locals = LocalCapture.CAPTURE_FAILEXCEPTION) | ||
public void handleRemaps(ResourceLocation name, ForgeRegistry<V> pool, | ||
List<RegistryEvent.MissingMappings.Mapping<V>> mappings, | ||
Map<ResourceLocation, Integer> missing, Map<ResourceLocation, Integer[]> remaps, | ||
Collection<ResourceLocation> defaulted, Collection<ResourceLocation> failed, | ||
boolean injectNetworkDummies, CallbackInfo ci, | ||
@Local RegistryEvent.MissingMappings.Mapping<V> remap, | ||
@Local(ordinal = 2) int realId) { | ||
if (remap.id != realId) { | ||
block(remap.id); | ||
remapped.put(remap.id, remap.getTarget().getRegistryName()); | ||
NomiLabs.LOGGER.warn( | ||
"[Forge Registry] Remap could not assign Id {} for Object {}! If this is of type BLOCK, without Data Fixers, after initial load, blocks will no longer be remapped!", | ||
remap.id, remap.getTarget().getRegistryName()); | ||
} | ||
} | ||
|
||
@Inject(method = "sync", at = @At("RETURN")) | ||
void syncBlockedRemapped(ResourceLocation name, ForgeRegistry<V> from, CallbackInfo ci) { | ||
blocked.clear(); | ||
|
||
var remFrom = (RemappableForgeRegistry) from; | ||
|
||
remFrom.getBlocked().forEach(this::block); | ||
|
||
remapped.clear(); | ||
remFrom.getRemapped().forEach(this::addRemapped); | ||
} | ||
|
||
@Inject(method = "makeSnapshot", at = @At("RETURN")) | ||
public void addRemappedToSnapshot(CallbackInfoReturnable<ForgeRegistry.Snapshot> cir) { | ||
ForgeRegistry.Snapshot ret = cir.getReturnValue(); | ||
((RemappableSnapshot) ret).addAllRemapped(remapped); | ||
} | ||
|
||
@Override | ||
@Unique | ||
public void addRemapped(int id, ResourceLocation key) { | ||
remapped.put(id, key); | ||
} | ||
|
||
@Override | ||
@Unique | ||
public Map<Integer, ResourceLocation> getRemapped() { | ||
return remapped; | ||
} | ||
|
||
@Override | ||
@Unique | ||
public Set<Integer> getBlocked() { | ||
return blocked; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/nomiceu/nomilabs/mixin/GameDataMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.nomiceu.nomilabs.mixin; | ||
|
||
import java.util.Map; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.registries.ForgeRegistry; | ||
import net.minecraftforge.registries.GameData; | ||
import net.minecraftforge.registries.RegistryManager; | ||
|
||
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.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import com.llamalad7.mixinextras.sugar.Local; | ||
import com.nomiceu.nomilabs.mixinhelper.RemappableForgeRegistry; | ||
import com.nomiceu.nomilabs.mixinhelper.RemappableSnapshot; | ||
|
||
/** | ||
* This Mixin allows for the remapped and block lists to be loaded from the in-world-save. | ||
*/ | ||
@Mixin(value = GameData.class, remap = false) | ||
public class GameDataMixin { | ||
|
||
@Inject(method = "loadPersistentDataToStagingRegistry", | ||
at = @At(value = "INVOKE", | ||
target = "Lnet/minecraftforge/registries/ForgeRegistry;loadIds(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Lnet/minecraftforge/registries/ForgeRegistry;Lnet/minecraft/util/ResourceLocation;)V"), | ||
require = 1, | ||
locals = LocalCapture.CAPTURE_FAILEXCEPTION) | ||
private static void loadRemappedToRegistry(RegistryManager pool, RegistryManager to, | ||
Map<ResourceLocation, Integer[]> remaps, | ||
Map<ResourceLocation, Integer> missing, ResourceLocation name, | ||
ForgeRegistry.Snapshot snap, Class<?> regType, CallbackInfo ci, | ||
@Local(ordinal = 1) ForgeRegistry<?> newRegistry) { | ||
((RemappableSnapshot) snap).loadToRegistry((RemappableForgeRegistry) newRegistry); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/nomiceu/nomilabs/mixin/SnapshotMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.nomiceu.nomilabs.mixin; | ||
|
||
import java.util.Map; | ||
|
||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.nbt.NBTTagList; | ||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.common.util.Constants; | ||
import net.minecraftforge.registries.ForgeRegistry; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.nomiceu.nomilabs.mixinhelper.RemappableForgeRegistry; | ||
import com.nomiceu.nomilabs.mixinhelper.RemappableSnapshot; | ||
|
||
/** | ||
* This mixin allows for saving of Forge Registry Remappings to snapshots. | ||
*/ | ||
@Mixin(value = ForgeRegistry.Snapshot.class, remap = false) | ||
public class SnapshotMixin implements RemappableSnapshot { | ||
|
||
@Unique | ||
public Map<Integer, ResourceLocation> remapped = Maps.newHashMap(); | ||
|
||
@Unique | ||
private static final String REMAPPED_KEY = "remapped"; | ||
|
||
@Inject(method = "write", at = @At("RETURN")) | ||
public void saveRemapped(CallbackInfoReturnable<NBTTagCompound> cir) { | ||
NBTTagCompound data = cir.getReturnValue(); | ||
NBTTagList remapList = new NBTTagList(); | ||
remapped.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(e -> { | ||
NBTTagCompound tag = new NBTTagCompound(); | ||
tag.setInteger("K", e.getKey()); | ||
tag.setString("V", e.getValue().toString()); | ||
remapList.appendTag(tag); | ||
}); | ||
data.setTag(REMAPPED_KEY, remapList); | ||
} | ||
|
||
@Inject(method = "read", at = @At("RETURN")) | ||
private static void readRemapped(NBTTagCompound nbt, CallbackInfoReturnable<ForgeRegistry.Snapshot> cir) { | ||
ForgeRegistry.Snapshot ret = cir.getReturnValue(); | ||
NBTTagList list = nbt.getTagList(REMAPPED_KEY, Constants.NBT.TAG_COMPOUND); | ||
list.forEach(e -> { | ||
NBTTagCompound comp = (NBTTagCompound) e; | ||
((RemappableSnapshot) ret).addRemapped(comp.getInteger("K"), new ResourceLocation(comp.getString("V"))); | ||
}); | ||
} | ||
|
||
@Override | ||
@Unique | ||
public void addRemapped(int id, ResourceLocation key) { | ||
remapped.put(id, key); | ||
} | ||
|
||
@Override | ||
@Unique | ||
public void addAllRemapped(Map<Integer, ResourceLocation> map) { | ||
remapped.putAll(map); | ||
} | ||
|
||
@Override | ||
@Unique | ||
public void loadToRegistry(RemappableForgeRegistry reg) { | ||
remapped.forEach(reg::addRemapped); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/nomiceu/nomilabs/mixinhelper/RemappableForgeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.nomiceu.nomilabs.mixinhelper; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
|
||
public interface RemappableForgeRegistry { | ||
|
||
void addRemapped(int id, ResourceLocation key); | ||
|
||
Set<Integer> getBlocked(); | ||
|
||
Map<Integer, ResourceLocation> getRemapped(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/nomiceu/nomilabs/mixinhelper/RemappableSnapshot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.nomiceu.nomilabs.mixinhelper; | ||
|
||
import java.util.Map; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
|
||
public interface RemappableSnapshot { | ||
|
||
void addRemapped(int id, ResourceLocation key); | ||
|
||
void addAllRemapped(Map<Integer, ResourceLocation> map); | ||
|
||
void loadToRegistry(RemappableForgeRegistry reg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.