[Change] Prepared RecordItems
handling for 1.19.1
This commit is contained in:
parent
1f712d9493
commit
5d5442344c
5 changed files with 138 additions and 18 deletions
|
@ -4,6 +4,7 @@ import org.betterx.bclib.api.v2.ModIntegrationAPI;
|
||||||
import org.betterx.bclib.client.render.EmissiveTextureInfo;
|
import org.betterx.bclib.client.render.EmissiveTextureInfo;
|
||||||
import org.betterx.bclib.interfaces.BlockModelProvider;
|
import org.betterx.bclib.interfaces.BlockModelProvider;
|
||||||
import org.betterx.bclib.interfaces.ItemModelProvider;
|
import org.betterx.bclib.interfaces.ItemModelProvider;
|
||||||
|
import org.betterx.bclib.models.RecordItemModelProvider;
|
||||||
|
|
||||||
import com.mojang.datafixers.util.Pair;
|
import com.mojang.datafixers.util.Pair;
|
||||||
import net.minecraft.client.renderer.block.BlockModelShaper;
|
import net.minecraft.client.renderer.block.BlockModelShaper;
|
||||||
|
@ -58,16 +59,23 @@ public class CustomModelBakery {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Registry.ITEM.stream().parallel().filter(item -> item instanceof ItemModelProvider).forEach(item -> {
|
Registry.ITEM.stream()
|
||||||
ResourceLocation registryID = Registry.ITEM.getKey(item);
|
.parallel()
|
||||||
ResourceLocation storageID = new ResourceLocation(
|
.filter(item -> item instanceof ItemModelProvider || RecordItemModelProvider.has(item))
|
||||||
registryID.getNamespace(),
|
.forEach(item -> {
|
||||||
"models/item/" + registryID.getPath() + ".json"
|
ResourceLocation registryID = Registry.ITEM.getKey(item);
|
||||||
);
|
ResourceLocation storageID = new ResourceLocation(
|
||||||
if (resourceManager.getResource(storageID).isEmpty()) {
|
registryID.getNamespace(),
|
||||||
addItemModel(registryID, (ItemModelProvider) item);
|
"models/item/" + registryID.getPath() + ".json"
|
||||||
}
|
);
|
||||||
});
|
final ItemModelProvider provider = (item instanceof ItemModelProvider)
|
||||||
|
? (ItemModelProvider) item
|
||||||
|
: RecordItemModelProvider.get(item);
|
||||||
|
|
||||||
|
if (resourceManager.getResource(storageID).isEmpty()) {
|
||||||
|
addItemModel(registryID, provider);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addBlockModel(ResourceLocation blockID, Block block) {
|
private void addBlockModel(ResourceLocation blockID, Block block) {
|
||||||
|
|
|
@ -1,17 +1,75 @@
|
||||||
package org.betterx.bclib.items;
|
package org.betterx.bclib.items;
|
||||||
|
|
||||||
import org.betterx.bclib.interfaces.ItemModelProvider;
|
import org.betterx.bclib.BCLib;
|
||||||
|
|
||||||
import net.minecraft.sounds.SoundEvent;
|
import net.minecraft.sounds.SoundEvent;
|
||||||
import net.minecraft.world.item.RecordItem;
|
import net.minecraft.world.item.RecordItem;
|
||||||
|
|
||||||
public class BaseDiscItem extends RecordItem implements ItemModelProvider {
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
|
public class BaseDiscItem extends RecordItem {
|
||||||
|
/**
|
||||||
|
* @param comparatorOutput
|
||||||
|
* @param sound
|
||||||
|
* @param settings
|
||||||
|
* @deprecated Please use {@link BaseDiscItem#create(int, SoundEvent, Properties, int)} instead
|
||||||
|
*/
|
||||||
@Deprecated(forRemoval = true)
|
@Deprecated(forRemoval = true)
|
||||||
public BaseDiscItem(int comparatorOutput, SoundEvent sound, Properties settings) {
|
public BaseDiscItem(int comparatorOutput, SoundEvent sound, Properties settings) {
|
||||||
this(comparatorOutput, sound, settings, 30);
|
this(comparatorOutput, sound, settings, 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param comparatorOutput
|
||||||
|
* @param sound
|
||||||
|
* @param settings
|
||||||
|
* @param lengthInSeconds
|
||||||
|
* @deprecated Please use {@link BaseDiscItem#create(int, SoundEvent, Properties, int)} instead
|
||||||
|
*/
|
||||||
|
@Deprecated(forRemoval = true)
|
||||||
public BaseDiscItem(int comparatorOutput, SoundEvent sound, Properties settings, int lengthInSeconds) {
|
public BaseDiscItem(int comparatorOutput, SoundEvent sound, Properties settings, int lengthInSeconds) {
|
||||||
super(comparatorOutput, sound, settings);
|
super(comparatorOutput, sound, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static RecordItem create(int comparatorOutput, SoundEvent sound, Properties settings, int lengthInSeconds) {
|
||||||
|
for (Constructor<?> c : RecordItem.class.getConstructors()) {
|
||||||
|
if (c.getParameterCount() == 4) {
|
||||||
|
var types = c.getParameterTypes();
|
||||||
|
if (types.length == 4) { //1.19.1 Constructor
|
||||||
|
if (
|
||||||
|
types[0].isAssignableFrom(int.class)
|
||||||
|
&& types[1].isAssignableFrom(SoundEvent.class)
|
||||||
|
&& types[2].isAssignableFrom(Properties.class)
|
||||||
|
&& types[3].isAssignableFrom(int.class)
|
||||||
|
) {
|
||||||
|
c.setAccessible(true);
|
||||||
|
try {
|
||||||
|
return (RecordItem) c.newInstance(comparatorOutput, sound, settings, lengthInSeconds);
|
||||||
|
} catch (Exception e) {
|
||||||
|
BCLib.LOGGER.error("Failed to instantiate RecordItem", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (c.getParameterCount() == 3) {
|
||||||
|
var types = c.getParameterTypes();
|
||||||
|
if (types.length == 3) { //1.19 constructor
|
||||||
|
if (
|
||||||
|
types[0].isAssignableFrom(int.class)
|
||||||
|
&& types[1].isAssignableFrom(SoundEvent.class)
|
||||||
|
&& types[2].isAssignableFrom(Properties.class)
|
||||||
|
) {
|
||||||
|
c.setAccessible(true);
|
||||||
|
try {
|
||||||
|
return (RecordItem) c.newInstance(comparatorOutput, sound, settings);
|
||||||
|
} catch (Exception e) {
|
||||||
|
BCLib.LOGGER.error("Failed to instantiate RecordItem", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BCLib.LOGGER.error("No Constructor for RecordItems found.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package org.betterx.bclib.models;
|
||||||
|
|
||||||
|
import org.betterx.bclib.interfaces.ItemModelProvider;
|
||||||
|
|
||||||
|
import net.minecraft.server.packs.resources.ResourceManager;
|
||||||
|
import net.minecraft.world.item.Item;
|
||||||
|
import net.minecraft.world.item.RecordItem;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This was just added because the constructor of a RecordItem from 1.19 to 1.19.1 changed
|
||||||
|
* and we only need to add the {@link ItemModelProvider} interface.
|
||||||
|
* <p>
|
||||||
|
* In order to keep cross version compat for bclib we choose to use reflection to instanciate
|
||||||
|
* RecordItem, but we need an additional registry that will provide the {@link ItemModelProvider}
|
||||||
|
* for RecordItems.
|
||||||
|
* <p>
|
||||||
|
* This class (and and all according changes in
|
||||||
|
* {@link org.betterx.bclib.client.models.CustomModelBakery#loadCustomModels(ResourceManager)} can
|
||||||
|
* be abandoned when we drop support for 1.19.
|
||||||
|
*/
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public class RecordItemModelProvider {
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public static final ItemModelProvider DEFAULT_PROVIDER = new ItemModelProvider() {
|
||||||
|
};
|
||||||
|
private static final Map<Item, ItemModelProvider> PROIVDER_MAPPPING = new HashMap<>();
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public static void add(RecordItem record) {
|
||||||
|
if (record != null) {
|
||||||
|
PROIVDER_MAPPPING.put(record, DEFAULT_PROVIDER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public static boolean has(Item i) {
|
||||||
|
return PROIVDER_MAPPPING.containsKey(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public static ItemModelProvider get(Item i) {
|
||||||
|
return PROIVDER_MAPPPING.get(i);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ import org.betterx.bclib.items.tool.BaseAxeItem;
|
||||||
import org.betterx.bclib.items.tool.BaseHoeItem;
|
import org.betterx.bclib.items.tool.BaseHoeItem;
|
||||||
import org.betterx.bclib.items.tool.BasePickaxeItem;
|
import org.betterx.bclib.items.tool.BasePickaxeItem;
|
||||||
import org.betterx.bclib.items.tool.BaseShearsItem;
|
import org.betterx.bclib.items.tool.BaseShearsItem;
|
||||||
|
import org.betterx.bclib.models.RecordItemModelProvider;
|
||||||
import org.betterx.worlds.together.tag.v3.CommonItemTags;
|
import org.betterx.worlds.together.tag.v3.CommonItemTags;
|
||||||
import org.betterx.worlds.together.tag.v3.TagManager;
|
import org.betterx.worlds.together.tag.v3.TagManager;
|
||||||
import org.betterx.worlds.together.tag.v3.ToolTags;
|
import org.betterx.worlds.together.tag.v3.ToolTags;
|
||||||
|
@ -34,12 +35,14 @@ public class ItemRegistry extends BaseRegistry<Item> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Item registerDisc(ResourceLocation itemId, int power, SoundEvent sound, int lengthInSeconds) {
|
public Item registerDisc(ResourceLocation itemId, int power, SoundEvent sound, int lengthInSeconds) {
|
||||||
BaseDiscItem item = new BaseDiscItem(power, sound, makeItemSettings().stacksTo(1), lengthInSeconds);
|
RecordItem item = BaseDiscItem.create(power, sound, makeItemSettings().stacksTo(1), lengthInSeconds);
|
||||||
|
if (item != null) {
|
||||||
if (!config.getBoolean("musicDiscs", itemId.getPath(), true)) {
|
RecordItemModelProvider.add(item);
|
||||||
return item;
|
if (!config.getBoolean("musicDiscs", itemId.getPath(), true)) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
register(itemId, item);
|
||||||
}
|
}
|
||||||
register(itemId, item);
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,10 @@
|
||||||
"depends": {
|
"depends": {
|
||||||
"fabricloader": ">=0.14.6",
|
"fabricloader": ">=0.14.6",
|
||||||
"fabric": ">=0.56.0",
|
"fabric": ">=0.56.0",
|
||||||
"minecraft": "1.19"
|
"minecraft": [
|
||||||
|
"1.19",
|
||||||
|
"1.19.1"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"custom": {
|
"custom": {
|
||||||
"modmenu": {
|
"modmenu": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue