Skip to content

Commit 81a0a05

Browse files
authored
Texture API v1 and fix some broken VanillaIds (#37)
* backup of progress just in case * remap event finalise * Start models * start model discovery * move testmod * ... * merge * move test code around a bit * readd atlas mixins by actually adding resources * actually provide correct ep * hahahaha yes * :yort-fuckin-haw: * fuck I broke something * fix vanillaids * correct ids pls * corelib compat * progress, still got to get the model loader debugged but it's at least fixable by the end of the week * successful loading * Test model * avoid npe, but there are a few... errors * todo: wtf * fix choco's generated texture atlases * e * add another test block because why not * fix testmod crash * cube_top_bottom * refactrz and change to 1 blocc * fix up test mods * JModel -> ModelJson * whitespace * should fix choco's requests * comment * whitespace * choco changes
1 parent 664af04 commit 81a0a05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1062
-167
lines changed

legacy-registries-v1/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ archivesBaseName = 'legacy-registries-v1'
22
version = getSubprojectVersion(project, '1.1.0')
33

44
moduleDependencies(project, 'legacy-api-base', 'legacy-networking-v0')
5+
56
dependencies {
7+
testImplementation project(path: ':legacy-recipes-v0')
8+
testImplementation project(path: ':legacy-translations-v0')
69
}

legacy-registries-v1/src/main/java/io/github/minecraftcursedlegacy/api/registry/Registry.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map.Entry;
3030
import java.util.Set;
3131
import java.util.Spliterator;
32+
import java.util.function.BiConsumer;
3233
import java.util.function.Consumer;
3334
import java.util.function.IntFunction;
3435

@@ -39,6 +40,7 @@
3940
import com.google.common.collect.HashBiMap;
4041

4142
import io.github.minecraftcursedlegacy.impl.registry.RegistryImpl;
43+
import io.github.minecraftcursedlegacy.impl.registry.sync.RegistryDiffImpl;
4244
import io.github.minecraftcursedlegacy.impl.registry.sync.RegistryRemapper;
4345
import net.fabricmc.fabric.api.event.Event;
4446
import net.minecraft.util.io.CompoundTag;
@@ -59,6 +61,7 @@ public Registry(Class<T> clazz, Id registryName, @Nullable T defaultValue) {
5961
RegistryRemapper.addRegistry(this);
6062

6163
this.event = RegistryImpl.createEvent(clazz);
64+
this.remapEvent = RegistryImpl.createRemapEvent(clazz);
6265
}
6366

6467
protected final BiMap<Id, T> byRegistryId = HashBiMap.create();
@@ -67,6 +70,10 @@ public Registry(Class<T> clazz, Id registryName, @Nullable T defaultValue) {
6770
@Nullable
6871
private final T defaultValue;
6972
private final Event<RegistryEntryAddedCallback<T>> event;
73+
// Remap stuff
74+
private RegistryDiffImpl<T> lastDiff;
75+
private final Event<RegistryRemappedCallback<T>> remapEvent;
76+
7077
private int nextId = this.getStartSerialisedId();
7178
/**
7279
* Whether the registry is locked, and values can no longer be registered to it.
@@ -191,6 +198,7 @@ public final CompoundTag remap(CompoundTag tag) {
191198
this.beforeRemap(tag);
192199
List<Entry<Id, T>> unmapped = new ArrayList<>();
193200
Set<Entry<Id, T>> toMap = this.byRegistryId.entrySet();
201+
this.lastDiff = new RegistryDiffImpl<>(this.bySerialisedId);
194202
this.bySerialisedId.clear();
195203

196204
// remap serialised ids
@@ -202,6 +210,7 @@ public final CompoundTag remap(CompoundTag tag) {
202210

203211
int newSerialisedId = tag.getInt(key);
204212
this.bySerialisedId.put(newSerialisedId, value);
213+
this.lastDiff.addEntry(newSerialisedId, value);
205214
this.onRemap(value, newSerialisedId);
206215
result.put(key, newSerialisedId); // add to new tag
207216
} else {
@@ -234,6 +243,7 @@ protected void addNewValues(List<Entry<Id, T>> unmapped, CompoundTag tag) {
234243

235244
// readd to registry
236245
this.bySerialisedId.put(serialisedId, value);
246+
this.lastDiff.addEntry(serialisedId, value);
237247
// add to tag
238248
tag.put(entry.getKey().toString(), serialisedId);
239249
this.onRemap(value, serialisedId);
@@ -274,8 +284,10 @@ protected void beforeRemap(CompoundTag tag) {
274284
/**
275285
* Called before registry remapping for this registry.
276286
* Override this to add finalisations for after registry remapping.
287+
* The default implementation invokes this registry's {@linkplain RegistryRemappedCallback}.
277288
*/
278289
protected void postRemap() {
290+
this.getRemapEvent().invoker().onRemap(this, this.lastDiff);
279291
}
280292

281293
/**
@@ -324,17 +336,38 @@ public final Event<RegistryEntryAddedCallback<T>> getEvent() {
324336
return this.event;
325337
}
326338

339+
/**
340+
* @return the {@linkplain RegistryEntryAddedCallback} event associated with this registry.
341+
* @since 1.1.0
342+
*/
343+
public final Event<RegistryRemappedCallback<T>> getRemapEvent() {
344+
return this.remapEvent;
345+
}
346+
327347
@Override
328348
@Nonnull
329349
public Iterator<T> iterator() {
330350
return this.values().iterator();
331351
}
332352

353+
/**
354+
* Iterate over the registry.
355+
* @param consumer the callback function.
356+
*/
333357
@Override
334358
public void forEach(Consumer<? super T> consumer) {
335359
this.values().forEach(consumer);
336360
}
337361

362+
/**
363+
* Iterate over pairs of registry id - tile in this registry.
364+
* @param consumer the callback function.
365+
* @since 1.1.0
366+
*/
367+
public void forEach(BiConsumer<Id, ? super T> consumer) {
368+
this.byRegistryId.forEach(consumer);
369+
}
370+
338371
@Override
339372
public Spliterator<T> spliterator() {
340373
return this.values().spliterator();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2020 The Cursed Legacy Team.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.github.minecraftcursedlegacy.api.registry;
25+
26+
import javax.annotation.Nullable;
27+
28+
/**
29+
* Interface look at the difference between a registry before and after remapping.
30+
* @since 1.1.0
31+
*/
32+
public interface RegistryDiff<T> {
33+
/**
34+
* Retrieves the new serialised id by the old one.
35+
* @param old the old serialised id.
36+
* @return the new serialised id, if present
37+
*/
38+
@Nullable
39+
Integer getNewSerialisedId(int old);
40+
/**
41+
* Retrieves the object associated with an old serialised id.
42+
* @param old the old serialised id.
43+
* @return the object that was associated with it.
44+
*/
45+
@Nullable
46+
T getByOldSerialisedId(int old);
47+
/**
48+
* Retrieves the old serialised id that was associated with an object.
49+
* @param value the object to retrieve the id for.
50+
* @return the old serialised id that was associated with an object.
51+
*/
52+
int getOldSerialisedId(T value);
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2020 The Cursed Legacy Team.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.github.minecraftcursedlegacy.api.registry;
25+
26+
/**
27+
* Runs after registry entries are remapped to allow for mods to dynamically handle registry objects. <br/>
28+
* Get the event for this callback from a given registry using {@linkplain Registry#getRemapEvent}.
29+
* @since 1.1.0
30+
*/
31+
@FunctionalInterface
32+
public interface RegistryRemappedCallback<T> {
33+
/**
34+
* Called when the registry is remapped.
35+
* @param registry the registry which has been remapped.
36+
* @param diff an interface look into the difference between the old and new registry raw ids.
37+
*/
38+
void onRemap(Registry<T> registry, RegistryDiff<T> diff);
39+
}

legacy-registries-v1/src/main/java/io/github/minecraftcursedlegacy/impl/registry/ItemTypeRegistry.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
import java.util.Map.Entry;
3030
import java.util.function.IntFunction;
3131

32-
import javax.annotation.Nullable;
33-
3432
import io.github.minecraftcursedlegacy.accessor.registry.AccessorPlaceableTileItem;
3533
import io.github.minecraftcursedlegacy.accessor.registry.AccessorRecipeRegistry;
3634
import io.github.minecraftcursedlegacy.accessor.registry.AccessorShapedRecipe;
3735
import io.github.minecraftcursedlegacy.accessor.registry.AccessorShapelessRecipe;
3836
import io.github.minecraftcursedlegacy.accessor.registry.AccessorTileItem;
3937
import io.github.minecraftcursedlegacy.api.registry.Id;
4038
import io.github.minecraftcursedlegacy.api.registry.Registry;
41-
import io.github.minecraftcursedlegacy.impl.registry.client.AtlasMapper;
4239
import io.github.minecraftcursedlegacy.impl.registry.sync.RegistryRemapper;
4340
import net.minecraft.item.ItemInstance;
4441
import net.minecraft.item.ItemType;
@@ -62,17 +59,14 @@ class ItemTypeRegistry extends Registry<ItemType> {
6259
for (int i = 0; i < ItemType.byId.length; ++i) {
6360
ItemType value = ItemType.byId[i];
6461

65-
@Nullable
66-
Tile tile = null;
67-
6862
if (value instanceof TileItem) {
69-
RegistryImpl.T_2_TI.put(tile = Tile.BY_ID[((AccessorTileItem) value).getTileId()], (TileItem) value);
63+
RegistryImpl.T_2_TI.put(Tile.BY_ID[((AccessorTileItem) value).getTileId()], (TileItem) value);
7064
} else if (value instanceof PlaceableTileItem) {
71-
RegistryImpl.T_2_TI.put(tile = Tile.BY_ID[((AccessorPlaceableTileItem) value).getTileId()], (PlaceableTileItem) value);
65+
RegistryImpl.T_2_TI.put(Tile.BY_ID[((AccessorPlaceableTileItem) value).getTileId()], (PlaceableTileItem) value);
7266
}
7367

7468
if (value != null) {
75-
this.byRegistryId.put(tile == null ? VanillaIds.getVanillaId(value) : VanillaIds.getVanillaId(tile), value);
69+
this.byRegistryId.put(VanillaIds.getVanillaId(value), value);
7670
this.bySerialisedId.put(i, value);
7771
}
7872
}
@@ -206,8 +200,8 @@ protected void postRemap() {
206200

207201
((SmeltingRecipeSetter) smelting).setRecipes(newRecipes);
208202

209-
RegistryRemapper.LOGGER.info("Remapping custom texture atlases.");
210-
AtlasMapper.onRegistryRemap(this.oldItemTypes);
203+
// Invoke remap event
204+
super.postRemap();
211205
}
212206

213207
@Override

legacy-registries-v1/src/main/java/io/github/minecraftcursedlegacy/impl/registry/RegistryImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.github.minecraftcursedlegacy.api.registry.Id;
3434
import io.github.minecraftcursedlegacy.api.registry.Registry;
3535
import io.github.minecraftcursedlegacy.api.registry.RegistryEntryAddedCallback;
36+
import io.github.minecraftcursedlegacy.api.registry.RegistryRemappedCallback;
3637
import io.github.minecraftcursedlegacy.impl.Hacks;
3738
import io.github.minecraftcursedlegacy.impl.registry.sync.RegistrySyncChannelS2C;
3839
import net.fabricmc.api.ModInitializer;
@@ -63,6 +64,14 @@ public static <T> Event<RegistryEntryAddedCallback<T>> createEvent(Class<T> claz
6364
});
6465
}
6566

67+
public static <T> Event<RegistryRemappedCallback<T>> createRemapEvent(Class<T> clazz) {
68+
return EventFactory.createArrayBacked(RegistryRemappedCallback.class, listeners -> (registry, diff) -> {
69+
for (RegistryRemappedCallback<T> listener : listeners) {
70+
listener.onRemap(registry, diff);
71+
}
72+
});
73+
}
74+
6675
static int nextItemTypeId() {
6776
while (ItemType.byId[currentItemtypeId] != null) {
6877
++currentItemtypeId;

legacy-registries-v1/src/main/java/io/github/minecraftcursedlegacy/impl/registry/VanillaIds.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static void initialiseTiles() {
112112
set(DISPENSER, "dispenser");
113113
set(SANDSTONE, "sandstone");
114114
set(NOTEBLOCK, "noteblock");
115-
set(BED, "bed");
115+
set(BED, "bed", true);
116116
set(GOLDEN_RAIL, "powered_rail");
117117
set(DETECTOR_RAIL, "detector_rail");
118118
set(STICKY_PISTON, "sticky_piston");
@@ -131,7 +131,7 @@ static void initialiseTiles() {
131131
set(BLOCK_IRON, "block_iron");
132132
set(DOUBLE_STONE_SLAB, "double_stone_slab");
133133
set(STONE_SLAB, "stone_slab");
134-
set(BRICK, "brick");
134+
set(BRICK, "bricks");
135135
set(TNT, "tnt");
136136
set(BOOKSHELF, "bookshelf");
137137
set(MOSSY_COBBLESTONE, "mossy_cobblestone");
@@ -150,14 +150,14 @@ static void initialiseTiles() {
150150
set(FURNACE, "furnace");
151151
set(FURNACE_LIT, "lit_furnace");
152152
set(STANDING_SIGN, "standing_sign");
153-
set(DOOR_WOOD, "wooden_door");
153+
set(DOOR_WOOD, "wooden_door", true);
154154
set(LADDER, "ladder");
155155
set(RAIL, "rail");
156156
set(STAIRS_STONE, "stone_stairs");
157157
set(WALL_SIGN, "wall_sign");
158158
set(LEVER, "lever");
159159
set(WOODEN_PRESSURE_PLATE, "wooden_pressure_plate");
160-
set(DOOR_IRON, "iron_door");
160+
set(DOOR_IRON, "iron_door", true);
161161
set(STONE_PRESSURE_PLATE, "stone_pressure_plate");
162162
set(REDSTONE_ORE, "redstone_ore");
163163
set(REDSTONE_ORE_LIT, "lit_redstone_ore");
@@ -168,8 +168,8 @@ static void initialiseTiles() {
168168
set(ICE, "ice");
169169
set(SNOW_BLOCK, "snow_block");
170170
set(CACTUS, "cactus");
171-
set(CLAY, "clay");
172-
set(REEDS, "reeds");
171+
set(CLAY, "clay_block");
172+
set(REEDS, "reeds", true);
173173
set(JUKEBOX, "jukebox");
174174
set(FENCE, "fence");
175175
set(PUMPKIN, "pumpkin");
@@ -178,9 +178,9 @@ static void initialiseTiles() {
178178
set(GLOWSTONE, "glowstone");
179179
set(PORTAL, "portal");
180180
set(LIT_PUMPKIN, "lit_pumpkin");
181-
set(CAKE, "cake");
182-
set(REDSTONE_REPEATER, "redstone_repeater");
183-
set(REDSTONE_REPEATER_LIT, "lit_redstone_repeater");
181+
set(CAKE, "cake", true);
182+
set(REDSTONE_REPEATER, "redstone_repeater", true);
183+
set(REDSTONE_REPEATER_LIT, "lit_redstone_repeater", true);
184184
set(LOCKED_CHEST, "locked_chest");
185185
set(TRAPDOOR, "trapdoor");
186186
}
@@ -299,8 +299,15 @@ static void initialiseItems() {
299299
}
300300

301301
private static void set(Tile tile, String id) {
302+
set(tile, id, false);
303+
}
304+
305+
private static void set(Tile tile, String id, boolean alterPlaceableId) {
302306
Id id_ = new Id(id);
307+
Id item_id_ = alterPlaceableId ? new Id(id + "_block") : id_;
308+
303309
TILE_IDS.put(tile, id_);
310+
ITEM_IDS.put(ItemType.byId[tile.id], item_id_);
304311

305312
// legacy
306313
String idPart = tile.method_1597();
@@ -314,7 +321,7 @@ private static void set(Tile tile, String id) {
314321
Id legacy = new Id(idPart + "_" + tile.id);
315322

316323
TILE_LEGACY_COMPAT.put(legacy, id_);
317-
ITEM_LEGACY_COMPAT.put(legacy, id_); // for tile items
324+
ITEM_LEGACY_COMPAT.put(legacy, item_id_); // for tile items
318325
}
319326

320327
private static void set(ItemType item, String id) {

0 commit comments

Comments
 (0)