Change loading models (WIP)

This commit is contained in:
Aleksey 2021-05-11 17:55:20 +03:00
parent ce98c5857e
commit 1e6451e375
9 changed files with 86 additions and 23 deletions

View file

@ -2,8 +2,10 @@ package ru.betterend.blocks.basis;
import java.io.Reader; import java.io.Reader;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import ru.betterend.patterns.Patterns; import ru.betterend.patterns.Patterns;
public class BarkBlock extends EndPillarBlock { public class BarkBlock extends EndPillarBlock {
@ -16,13 +18,19 @@ public class BarkBlock extends EndPillarBlock {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
return Patterns.createJson(Patterns.BLOCK_BASE, getName(blockId), blockId.getPath()); return Patterns.createJson(Patterns.BLOCK_BASE, getName(blockId), blockId.getPath());
} }
@Override @Override
public String getStatesPattern(Reader data) { public String getStatesPattern(Reader data) {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
return Patterns.createJson(data, getName(blockId), blockId.getPath()); return Patterns.createJson(data, getName(blockId), blockId.getPath());
} }
@Override
public BlockModel getBlockModel(BlockState blockState) {
return null;
}
private String getName(ResourceLocation blockId) { private String getName(ResourceLocation blockId) {
String name = blockId.getPath(); String name = blockId.getPath();
return name.replace("_bark", "_log_side"); return name.replace("_bark", "_log_side");

View file

@ -24,11 +24,6 @@ public class BlockBase extends Block implements BlockPatterned {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
// @Override
// public BlockModel getBlockModel(BlockState state) {
// return null;
// }
@Override @Override
public String getStatesPattern(Reader data) { public String getStatesPattern(Reader data) {
String block = Registry.BLOCK.getKey(this).getPath(); String block = Registry.BLOCK.getKey(this).getPath();
@ -36,13 +31,23 @@ public class BlockBase extends Block implements BlockPatterned {
} }
@Override @Override
public String getModelPattern(String block) { public String getModelString(String block) {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
return Patterns.createJson(Patterns.BLOCK_BASE, blockId.getPath(), block); return Patterns.createJson(Patterns.BLOCK_BASE, blockId.getPath(), blockId.getPath());
}
@Override
public BlockModel getItemModel() {
return getBlockModel(defaultBlockState());
} }
@Override @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_SIMPLE; return Patterns.STATE_SIMPLE;
} }
@Override
public BlockModel getBlockModel(BlockState blockState) {
return BlockModel.fromString(getModelString(""));
}
} }

View file

@ -7,7 +7,11 @@ import java.util.Map;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mojang.math.Transformation;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -69,6 +73,11 @@ public class EndAnvilBlock extends AnvilBlock implements BlockPatterned {
return Patterns.createJson(Patterns.BLOCK_ANVIL, map); return Patterns.createJson(Patterns.BLOCK_ANVIL, map);
} }
@Override
public BlockModel getItemModel() {
return getBlockModel(defaultBlockState());
}
protected String getTop(ResourceLocation blockId, String block) { protected String getTop(ResourceLocation blockId, String block) {
if (block.contains("item")) { if (block.contains("item")) {
return blockId.getPath() + "_top_0"; return blockId.getPath() + "_top_0";
@ -81,4 +90,18 @@ public class EndAnvilBlock extends AnvilBlock implements BlockPatterned {
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_ANVIL; return Patterns.STATE_ANVIL;
} }
@Override
public BlockModel getBlockModel(BlockState blockState) {
Direction facing = blockState.getValue(FACING);
int destruction = blockState.getValue(DESTRUCTION);
MultiVariant variant;
Transformation transform;
ResourceLocation blockId = Registry.BLOCK.getKey(this);
Map<String, String> map = Maps.newHashMap();
map.put("%anvil%", blockId.getPath());
map.put("%top%", "_top_" + destruction);
String jsonString = Patterns.createJson(Patterns.BLOCK_ANVIL, map);
return BlockModel.fromString(jsonString);
}
} }

View file

@ -5,6 +5,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -15,7 +16,7 @@ import net.minecraft.world.level.storage.loot.LootContext;
import ru.betterend.patterns.BlockPatterned; import ru.betterend.patterns.BlockPatterned;
import ru.betterend.patterns.Patterns; import ru.betterend.patterns.Patterns;
public class EndPillarBlock extends RotatedPillarBlock implements BlockPatterned { public abstract class EndPillarBlock extends RotatedPillarBlock implements BlockPatterned {
public EndPillarBlock(Properties settings) { public EndPillarBlock(Properties settings) {
super(settings); super(settings);
} }
@ -28,6 +29,11 @@ public class EndPillarBlock extends RotatedPillarBlock implements BlockPatterned
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override
public BlockModel getItemModel() {
return getBlockModel(defaultBlockState());
}
@Override @Override
public String getStatesPattern(Reader data) { public String getStatesPattern(Reader data) {

View file

@ -30,7 +30,7 @@ public class StrippableBarkBlock extends BarkBlock {
world.playSound(player, pos, SoundEvents.AXE_STRIP, SoundSource.BLOCKS, 1.0F, 1.0F); world.playSound(player, pos, SoundEvents.AXE_STRIP, SoundSource.BLOCKS, 1.0F, 1.0F);
if (!world.isClientSide) { if (!world.isClientSide) {
world.setBlock(pos, striped.defaultBlockState().setValue(RotatedPillarBlock.AXIS, state.getValue(RotatedPillarBlock.AXIS)), 11); world.setBlock(pos, striped.defaultBlockState().setValue(RotatedPillarBlock.AXIS, state.getValue(RotatedPillarBlock.AXIS)), 11);
if (player != null && !player.isCreative()) { if (!player.isCreative()) {
player.getMainHandItem().hurt(1, world.random, (ServerPlayer) player); player.getMainHandItem().hurt(1, world.random, (ServerPlayer) player);
} }
} }

View file

@ -1,5 +1,6 @@
package ru.betterend.mixin.client; package ru.betterend.mixin.client;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.ModelBakery; import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.client.resources.model.ModelResourceLocation;
@ -8,6 +9,7 @@ import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource; import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
@ -19,6 +21,7 @@ import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.patterns.BlockPatterned;
import ru.betterend.patterns.Patterned; import ru.betterend.patterns.Patterned;
import ru.betterend.world.generator.GeneratorOptions; import ru.betterend.world.generator.GeneratorOptions;
@ -41,28 +44,47 @@ public abstract class ModelLoaderMixin {
@Shadow @Shadow
protected abstract void cacheAndQueueDependencies(ResourceLocation resourceLocation, UnbakedModel unbakedModel); protected abstract void cacheAndQueueDependencies(ResourceLocation resourceLocation, UnbakedModel unbakedModel);
@Shadow
protected abstract BlockModel loadBlockModel(ResourceLocation resourceLocation);
@Inject(method = "loadModel", at = @At("HEAD"), cancellable = true) @Inject(method = "loadModel", at = @At("HEAD"), cancellable = true)
private void be_loadModels(ResourceLocation resourceLocation, CallbackInfo info) { private void be_loadModels(ResourceLocation resourceLocation, CallbackInfo info) {
if (BetterEnd.isModId(resourceLocation) && resourceLocation instanceof ModelResourceLocation) { if (BetterEnd.isModId(resourceLocation) && resourceLocation instanceof ModelResourceLocation) {
ResourceLocation clearLoc = new ResourceLocation(resourceLocation.getNamespace(), resourceLocation.getPath()); String modId = resourceLocation.getNamespace();
String path = resourceLocation.getPath();
ResourceLocation clearLoc = new ResourceLocation(modId, path);
ModelResourceLocation modelLoc = (ModelResourceLocation) resourceLocation; ModelResourceLocation modelLoc = (ModelResourceLocation) resourceLocation;
if (Objects.equals(modelLoc.getVariant(), "inventory")) { if (Objects.equals(modelLoc.getVariant(), "inventory")) {
ResourceLocation itemModelLoc = new ResourceLocation(resourceLocation.getNamespace(), "models/" + resourceLocation.getPath() + ".json"); ResourceLocation itemLoc = new ResourceLocation(modId, "item/" + path);
ResourceLocation itemModelLoc = new ResourceLocation(modId, "models/" + itemLoc.getPath() + ".json");
if (!resourceManager.hasResource(itemModelLoc)) { if (!resourceManager.hasResource(itemModelLoc)) {
Item item = Registry.ITEM.get(clearLoc); Item item = Registry.ITEM.get(clearLoc);
if (item instanceof Patterned) { if (item instanceof Patterned) {
BlockModel model = ((Patterned) item).getItemModel(); BlockModel model = ((Patterned) item).getItemModel();
ResourceLocation itemLoc = new ResourceLocation(resourceLocation.getNamespace(), "item/" + resourceLocation.getPath()); if (model != null) {
model.name = itemLoc.toString(); model.name = itemLoc.toString();
} else {
model = loadBlockModel(itemLoc);
}
cacheAndQueueDependencies(modelLoc, model); cacheAndQueueDependencies(modelLoc, model);
unbakedCache.put(modelLoc, model); unbakedCache.put(modelLoc, model);
info.cancel(); info.cancel();
} }
} }
} else { } else {
ResourceLocation blockstateId = new ResourceLocation(resourceLocation.getNamespace(), "blockstates/" + resourceLocation.getPath() + ".json"); ResourceLocation blockstateId = new ResourceLocation(modId, "blockstates/" + path + ".json");
if (!resourceManager.hasResource(blockstateId)) { if (!resourceManager.hasResource(blockstateId)) {
Block block = Registry.BLOCK.get(clearLoc);
if (block instanceof BlockPatterned) {
block.getStateDefinition().getPossibleStates().forEach(blockState -> {
UnbakedModel model = ((BlockPatterned) block).getBlockModel(blockState);
if (model != null) {
ModelResourceLocation stateLoc = BlockModelShaper.stateToModelLocation(clearLoc, blockState);
cacheAndQueueDependencies(stateLoc, model);
}
});
info.cancel();
}
} }
} }
} }

View file

@ -2,13 +2,12 @@ package ru.betterend.patterns;
import java.io.Reader; import java.io.Reader;
import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
public interface BlockPatterned extends Patterned { public interface BlockPatterned extends Patterned {
String getStatesPattern(Reader data); String getStatesPattern(Reader data);
ResourceLocation statePatternId(); ResourceLocation statePatternId();
@NotNull UnbakedModel getBlockModel(BlockState blockState); BlockModel getBlockModel(BlockState blockState);
} }

View file

@ -1,9 +1,8 @@
package ru.betterend.patterns; package ru.betterend.patterns;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import org.jetbrains.annotations.NotNull;
public interface Patterned { public interface Patterned {
String getModelString(String name); String getModelString(String name);
@NotNull BlockModel getItemModel(); BlockModel getItemModel();
} }

View file

@ -14,6 +14,7 @@ import com.google.common.collect.Maps;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import org.jetbrains.annotations.Nullable;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
public class Patterns { public class Patterns {
@ -156,7 +157,7 @@ public class Patterns {
} }
return json; return json;
} catch (Exception ex) { } catch (Exception ex) {
return null; return "";
} }
} }
} }