-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRegistryMixin.java
More file actions
87 lines (79 loc) · 3.05 KB
/
RegistryMixin.java
File metadata and controls
87 lines (79 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.denizenscript.clientizen.mixin;
import com.denizenscript.clientizen.access.RegistryMixinAccess;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException;
import com.mojang.serialization.Lifecycle;
import it.unimi.dsi.fastutil.objects.ObjectList;
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.SimpleRegistry;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryInfo;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
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 java.util.IdentityHashMap;
import java.util.Map;
@Mixin(SimpleRegistry.class)
public abstract class RegistryMixin<T> implements RegistryMixinAccess {
@Unique
boolean clientizen$isIntrusive;
@Shadow
private boolean frozen;
@Shadow
public abstract RegistryKey<? extends Registry<T>> getKey();
@Shadow
private @Nullable Map<T, RegistryEntry.Reference<T>> intrusiveValueToEntry;
@Shadow
@Final
private ObjectList<RegistryEntry.Reference<T>> rawIdToEntry;
@Shadow
@Final
private Map<Identifier, RegistryEntry.Reference<T>> idToEntry;
@Shadow
@Final
private Reference2IntMap<T> entryToRawId;
@Shadow
@Final
private Map<RegistryKey<T>, RegistryEntry.Reference<T>> keyToEntry;
@Shadow
@Final
private Map<T, RegistryEntry.Reference<T>> valueToEntry;
@Shadow
@Final
private Map<RegistryKey<T>, RegistryEntryInfo> keyToEntryInfo;
@Override
public void clientizen$unfreeze() {
if (!frozen) {
return;
}
frozen = false;
if (clientizen$isIntrusive) {
intrusiveValueToEntry = new IdentityHashMap<>();
}
}
@Override
public void clientizen$remove(Identifier toRemove) {
RegistryEntry.Reference<T> value = idToEntry.get(toRemove);
if (value == null) {
throw new InvalidArgumentsRuntimeException("Unable to remove '" + toRemove + "' from registry '" + getKey() + "': registry has no value by that key.");
}
RegistryKey<T> key = RegistryKey.of(getKey(), toRemove);
keyToEntry.remove(key);
idToEntry.remove(toRemove);
valueToEntry.remove(value.value());
rawIdToEntry.remove(value);
entryToRawId.removeInt(value.value());
keyToEntryInfo.remove(key);
}
@Inject(method = "<init>(Lnet/minecraft/registry/RegistryKey;Lcom/mojang/serialization/Lifecycle;Z)V", at = @At("TAIL"))
private void clientizen$saveIsIntrusive(RegistryKey<?> key, Lifecycle lifecycle, boolean intrusive, CallbackInfo ci) {
clientizen$isIntrusive = intrusive;
}
}