Code style fix, interface rename, custom item getter

This commit is contained in:
paulevsGitch 2021-07-20 00:10:00 +03:00
parent 179ada3296
commit c6afa74529
134 changed files with 3404 additions and 1244 deletions

View file

@ -6,8 +6,8 @@ import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Registry;
import ru.bclib.api.ModIntegrationAPI;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IPostInit;
import ru.bclib.interfaces.IRenderTyped;
import ru.bclib.interfaces.PostInitable;
import ru.bclib.interfaces.RenderLayerGetter;
import ru.bclib.registry.BaseBlockEntityRenders;
public class BCLibClient implements ClientModInitializer {
@ -17,8 +17,8 @@ public class BCLibClient implements ClientModInitializer {
BaseBlockEntityRenders.register();
registerRenderLayers();
Registry.BLOCK.forEach(block -> {
if (block instanceof IPostInit) {
((IPostInit) block).postInit();
if (block instanceof PostInitable) {
((PostInitable) block).postInit();
}
});
}
@ -27,8 +27,8 @@ public class BCLibClient implements ClientModInitializer {
RenderType cutout = RenderType.cutout();
RenderType translucent = RenderType.translucent();
Registry.BLOCK.forEach(block -> {
if (block instanceof IRenderTyped) {
BCLRenderLayer layer = ((IRenderTyped) block).getRenderLayer();
if (block instanceof RenderLayerGetter) {
BCLRenderLayer layer = ((RenderLayerGetter) block).getRenderLayer();
if (layer == BCLRenderLayer.CUTOUT) BlockRenderLayerMap.INSTANCE.putBlock(block, cutout);
else if (layer == BCLRenderLayer.TRANSLUCENT) BlockRenderLayerMap.INSTANCE.putBlock(block, translucent);
}

View file

@ -53,28 +53,48 @@ public class BlockSignEditScreen extends Screen {
protected void init() {
//set up a default model
model = new SignRenderer.SignModel(this.minecraft.getEntityModels().bakeLayer(ModelLayers.createSignModelName(WoodType.OAK)));
model = new SignRenderer.SignModel(this.minecraft.getEntityModels()
.bakeLayer(ModelLayers.createSignModelName(WoodType.OAK)));
minecraft.keyboardHandler.setSendRepeatsToGui(true);
this.addRenderableWidget(new Button(this.width / 2 - 100, this.height / 4 + 120, 200, 20, CommonComponents.GUI_DONE, (buttonWidget) -> {
this.finishEditing();
}));
this.addRenderableWidget(new Button(
this.width / 2 - 100,
this.height / 4 + 120,
200,
20,
CommonComponents.GUI_DONE,
(buttonWidget) -> {
this.finishEditing();
}
));
this.sign.setEditable(false);
this.selectionManager = new TextFieldHelper(() -> {
return this.text[this.currentRow];
}, (string) -> {
this.text[this.currentRow] = string;
this.sign.setMessage(this.currentRow, new TextComponent(string));
}, TextFieldHelper.createClipboardGetter(this.minecraft), TextFieldHelper.createClipboardSetter(this.minecraft), (string) -> {
return this.minecraft.font.width(string) <= 90;
});
this.selectionManager = new TextFieldHelper(
() -> {
return this.text[this.currentRow];
},
(string) -> {
this.text[this.currentRow] = string;
this.sign.setMessage(this.currentRow, new TextComponent(string));
},
TextFieldHelper.createClipboardGetter(this.minecraft),
TextFieldHelper.createClipboardSetter(this.minecraft),
(string) -> {
return this.minecraft.font.width(string) <= 90;
}
);
}
public void removed() {
minecraft.keyboardHandler.setSendRepeatsToGui(false);
ClientPacketListener clientPlayNetworkHandler = this.minecraft.getConnection();
if (clientPlayNetworkHandler != null) {
clientPlayNetworkHandler.send(new ServerboundSignUpdatePacket(this.sign.getBlockPos(), this.text[0], this.text[1], this.text[2], this.text[3]));
clientPlayNetworkHandler.send(new ServerboundSignUpdatePacket(
this.sign.getBlockPos(),
this.text[0],
this.text[1],
this.text[2],
this.text[3]
));
}
this.sign.setEditable(true);
@ -167,12 +187,36 @@ public class BlockSignEditScreen extends Screen {
}
float n = (float) (-this.minecraft.font.width(string2) / 2);
this.minecraft.font.drawInBatch(string2, n, (float) (m * 10 - this.text.length * 5), i, false, matrix4f, immediate, false, 0, 15728880, false);
this.minecraft.font.drawInBatch(
string2,
n,
(float) (m * 10 - this.text.length * 5),
i,
false,
matrix4f,
immediate,
false,
0,
15728880,
false
);
if (m == this.currentRow && j >= 0 && bl2) {
s = this.minecraft.font.width(string2.substring(0, Math.max(Math.min(j, string2.length()), 0)));
t = s - this.minecraft.font.width(string2) / 2;
if (j >= string2.length()) {
this.minecraft.font.drawInBatch("_", (float) t, (float) l, i, false, matrix4f, immediate, false, 0, 15728880, false);
this.minecraft.font.drawInBatch(
"_",
(float) t,
(float) l,
i,
false,
matrix4f,
immediate,
false,
0,
15728880,
false
);
}
}
}

View file

@ -23,31 +23,71 @@ public class BaseChestBlockModel {
MeshDefinition modelData = new MeshDefinition();
PartDefinition modelPartData = modelData.getRoot();
CubeDeformation deformation_partC = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partC", CubeListBuilder.create().texOffs(0, 19).addBox(1.0f, 0.0f, 1.0f, 14.0f, 9.0f, 14.0f, deformation_partC), PartPose.ZERO);
modelPartData.addOrReplaceChild(
"partC",
CubeListBuilder.create().texOffs(0, 19).addBox(1.0f, 0.0f, 1.0f, 14.0f, 9.0f, 14.0f, deformation_partC),
PartPose.ZERO
);
CubeDeformation deformation_partA = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partA", CubeListBuilder.create().texOffs(0, 0).addBox(1.0f, 0.0f, 0.0f, 14.0f, 5.0f, 14.0f, deformation_partA), PartPose.offset(0.0f, 9.0f, 1.0f));
modelPartData.addOrReplaceChild(
"partA",
CubeListBuilder.create().texOffs(0, 0).addBox(1.0f, 0.0f, 0.0f, 14.0f, 5.0f, 14.0f, deformation_partA),
PartPose.offset(0.0f, 9.0f, 1.0f)
);
CubeDeformation deformation_partB = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partB", CubeListBuilder.create().texOffs(0, 0).addBox(7.0f, -1.0f, 15.0f, 2.0f, 4.0f, 1.0f, deformation_partB), PartPose.offset(0.0f, 8.0f, 0.0f));
modelPartData.addOrReplaceChild(
"partB",
CubeListBuilder.create().texOffs(0, 0).addBox(7.0f, -1.0f, 15.0f, 2.0f, 4.0f, 1.0f, deformation_partB),
PartPose.offset(0.0f, 8.0f, 0.0f)
);
CubeDeformation deformation_partRightC = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partRightC", CubeListBuilder.create().texOffs(0, 19).addBox(1.0f, 0.0f, 1.0f, 15.0f, 9.0f, 14.0f, deformation_partRightC), PartPose.ZERO);
modelPartData.addOrReplaceChild(
"partRightC",
CubeListBuilder.create()
.texOffs(0, 19)
.addBox(1.0f, 0.0f, 1.0f, 15.0f, 9.0f, 14.0f, deformation_partRightC),
PartPose.ZERO
);
CubeDeformation deformation_partRightA = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partRightA", CubeListBuilder.create().texOffs(0, 0).addBox(1.0f, 0.0f, 0.0f, 15.0f, 5.0f, 14.0f, deformation_partRightA), PartPose.offset(0.0f, 9.0f, 1.0f));
modelPartData.addOrReplaceChild(
"partRightA",
CubeListBuilder.create().texOffs(0, 0).addBox(1.0f, 0.0f, 0.0f, 15.0f, 5.0f, 14.0f, deformation_partRightA),
PartPose.offset(0.0f, 9.0f, 1.0f)
);
CubeDeformation deformation_partRightB = new CubeDeformation(0.0f);
PartDefinition partRightB = modelPartData.addOrReplaceChild("partRightB", CubeListBuilder.create().texOffs(0, 0).addBox(15.0f, -1.0f, 15.0f, 1.0f, 4.0f, 1.0f, deformation_partRightB), PartPose.offset(0.0f, 8.0f, 0.0f));
PartDefinition partRightB = modelPartData.addOrReplaceChild(
"partRightB",
CubeListBuilder.create()
.texOffs(0, 0)
.addBox(15.0f, -1.0f, 15.0f, 1.0f, 4.0f, 1.0f, deformation_partRightB),
PartPose.offset(0.0f, 8.0f, 0.0f)
);
CubeDeformation deformation_partLeftC = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partLeftC", CubeListBuilder.create().texOffs(0, 19).addBox(0.0f, 0.0f, 1.0f, 15.0f, 9.0f, 14.0f, deformation_partLeftC), PartPose.ZERO);
modelPartData.addOrReplaceChild(
"partLeftC",
CubeListBuilder.create().texOffs(0, 19).addBox(0.0f, 0.0f, 1.0f, 15.0f, 9.0f, 14.0f, deformation_partLeftC),
PartPose.ZERO
);
CubeDeformation deformation_partLeftA = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partLeftA", CubeListBuilder.create().texOffs(0, 0).addBox(0.0f, 0.0f, 0.0f, 15.0f, 5.0f, 14.0f, deformation_partLeftA), PartPose.offset(0.0f, 9.0f, 1.0f));
modelPartData.addOrReplaceChild(
"partLeftA",
CubeListBuilder.create().texOffs(0, 0).addBox(0.0f, 0.0f, 0.0f, 15.0f, 5.0f, 14.0f, deformation_partLeftA),
PartPose.offset(0.0f, 9.0f, 1.0f)
);
CubeDeformation deformation_partLeftB = new CubeDeformation(0.0f);
modelPartData.addOrReplaceChild("partLeftB", CubeListBuilder.create().texOffs(0, 0).addBox(0.0f, -1.0f, 15.0f, 1.0f, 4.0f, 1.0f, deformation_partLeftB), PartPose.offset(0.0f, 8.0f, 0.0f));
modelPartData.addOrReplaceChild(
"partLeftB",
CubeListBuilder.create().texOffs(0, 0).addBox(0.0f, -1.0f, 15.0f, 1.0f, 4.0f, 1.0f, deformation_partLeftB),
PartPose.offset(0.0f, 8.0f, 0.0f)
);
return LayerDefinition.create(modelData, 64, 64);
}

View file

@ -1,45 +0,0 @@
package ru.bclib.client.models;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import ru.bclib.BCLib;
import java.util.Map;
import java.util.Optional;
import static net.minecraft.client.resources.model.ModelBakery.MISSING_MODEL_LOCATION;
public interface BlockModelProvider extends ItemModelProvider {
@Environment(EnvType.CLIENT)
default @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createBlockSimple(resourceLocation);
return ModelsHelper.fromPattern(pattern);
}
@Environment(EnvType.CLIENT)
default UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath());
registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createBlockSimple(modelId);
}
@Environment(EnvType.CLIENT)
default void registerBlockModel(ResourceLocation stateId, ResourceLocation modelId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
if (!modelCache.containsKey(modelId)) {
BlockModel model = getBlockModel(stateId, blockState);
if (model != null) {
model.name = modelId.toString();
modelCache.put(modelId, model);
}
else {
BCLib.LOGGER.warning("Error loading model: {}", modelId);
modelCache.put(modelId, modelCache.get(MISSING_MODEL_LOCATION));
}
}
}
}

View file

@ -1,13 +0,0 @@
package ru.bclib.client.models;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation;
public interface ItemModelProvider {
@Environment(EnvType.CLIENT)
default BlockModel getItemModel(ResourceLocation resourceLocation) {
return ModelsHelper.createItemModel(resourceLocation);
}
}

View file

@ -78,7 +78,12 @@ public class ModelsHelper {
}
public static MultiVariant createRandomTopModel(ResourceLocation resourceLocation) {
return new MultiVariant(Lists.newArrayList(new Variant(resourceLocation, Transformation.identity(), false, 1), new Variant(resourceLocation, BlockModelRotation.X0_Y90.getRotation(), false, 1), new Variant(resourceLocation, BlockModelRotation.X0_Y180.getRotation(), false, 1), new Variant(resourceLocation, BlockModelRotation.X0_Y270.getRotation(), false, 1)));
return new MultiVariant(Lists.newArrayList(
new Variant(resourceLocation, Transformation.identity(), false, 1),
new Variant(resourceLocation, BlockModelRotation.X0_Y90.getRotation(), false, 1),
new Variant(resourceLocation, BlockModelRotation.X0_Y180.getRotation(), false, 1),
new Variant(resourceLocation, BlockModelRotation.X0_Y270.getRotation(), false, 1)
));
}
public static class MultiPartBuilder {

View file

@ -52,7 +52,8 @@ public class PatternsHelper {
public static Optional<String> createJson(ResourceLocation patternId, Map<String, String> textures) {
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
try (InputStream input = resourceManager.getResource(patternId).getInputStream()) {
String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines().collect(Collectors.joining());
String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining());
for (Map.Entry<String, String> texture : textures.entrySet()) {
json = json.replace(texture.getKey(), texture.getValue());
}

View file

@ -51,7 +51,11 @@ public class BaseChestBlockEntityRenderer implements BlockEntityRenderer<BaseChe
public void render(BaseChestBlockEntity entity, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light, int overlay) {
Level world = entity.getLevel();
boolean worldExists = world != null;
BlockState blockState = worldExists ? entity.getBlockState() : Blocks.CHEST.defaultBlockState().setValue(ChestBlock.FACING, Direction.SOUTH);
BlockState blockState = worldExists ? entity.getBlockState() : Blocks.CHEST.defaultBlockState()
.setValue(
ChestBlock.FACING,
Direction.SOUTH
);
ChestType chestType = blockState.hasProperty(ChestBlock.TYPE) ? blockState.getValue(ChestBlock.TYPE) : ChestType.SINGLE;
Block block = blockState.getBlock();
if (block instanceof AbstractChestBlock) {
@ -72,23 +76,54 @@ public class BaseChestBlockEntityRenderer implements BlockEntityRenderer<BaseChe
propertySource = DoubleBlockCombiner.Combiner::acceptNone;
}
float pitch = ((Float2FloatFunction) propertySource.apply(ChestBlock.opennessCombiner(entity))).get(tickDelta);
float pitch = ((Float2FloatFunction) propertySource.apply(ChestBlock.opennessCombiner(entity))).get(
tickDelta);
pitch = 1.0F - pitch;
pitch = 1.0F - pitch * pitch * pitch;
@SuppressWarnings({"unchecked", "rawtypes"}) int blockLight = ((Int2IntFunction) propertySource.apply(new BrightnessCombiner())).applyAsInt(light);
@SuppressWarnings({
"unchecked",
"rawtypes"
}) int blockLight = ((Int2IntFunction) propertySource.apply(new BrightnessCombiner())).applyAsInt(light);
VertexConsumer vertexConsumer = getConsumer(vertexConsumers, block, chestType);
if (isDouble) {
if (chestType == ChestType.LEFT) {
renderParts(matrices, vertexConsumer, chestModel.partLeftA, chestModel.partLeftB, chestModel.partLeftC, pitch, blockLight, overlay);
renderParts(
matrices,
vertexConsumer,
chestModel.partLeftA,
chestModel.partLeftB,
chestModel.partLeftC,
pitch,
blockLight,
overlay
);
}
else {
renderParts(matrices, vertexConsumer, chestModel.partRightA, chestModel.partRightB, chestModel.partRightC, pitch, blockLight, overlay);
renderParts(
matrices,
vertexConsumer,
chestModel.partRightA,
chestModel.partRightB,
chestModel.partRightC,
pitch,
blockLight,
overlay
);
}
}
else {
renderParts(matrices, vertexConsumer, chestModel.partA, chestModel.partB, chestModel.partC, pitch, blockLight, overlay);
renderParts(
matrices,
vertexConsumer,
chestModel.partA,
chestModel.partB,
chestModel.partC,
pitch,
blockLight,
overlay
);
}
matrices.popPose();
@ -120,10 +155,24 @@ public class BaseChestBlockEntityRenderer implements BlockEntityRenderer<BaseChe
ResourceLocation blockId = Registry.BLOCK.getKey(block);
String modId = blockId.getNamespace();
String path = blockId.getPath();
LAYERS.put(block, new RenderType[] {RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + ".png")), RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_left.png")), RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_right.png"))});
LAYERS.put(
block,
new RenderType[] {
RenderType.entityCutout(new ResourceLocation(
modId,
"textures/entity/chest/" + path + ".png"
)),
RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_left.png")),
RenderType.entityCutout(new ResourceLocation(modId, "textures/entity/chest/" + path + "_right.png"))
}
);
}
static {
defaultLayer = new RenderType[] {RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal.png")), RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_left.png")), RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_right.png"))};
defaultLayer = new RenderType[] {
RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal.png")),
RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_left.png")),
RenderType.entityCutout(new ResourceLocation("textures/entity/chest/normal_right.png"))
};
}
}

View file

@ -87,10 +87,14 @@ public class BaseSignBlockEntityRenderer implements BlockEntityRenderer<BaseSign
int p = (int) (NativeImage.getB(m) * 0.4D);
int q = NativeImage.combine(0, p, o, n);
FormattedCharSequence[] formattedCharSequences = signBlockEntity.getRenderMessages(Minecraft.getInstance().isTextFilteringEnabled(), (component) -> {
List<FormattedCharSequence> list = this.font.split(component, 90);
return list.isEmpty() ? FormattedCharSequence.EMPTY : (FormattedCharSequence) list.get(0);
});
FormattedCharSequence[] formattedCharSequences = signBlockEntity.getRenderMessages(
Minecraft.getInstance()
.isTextFilteringEnabled(),
(component) -> {
List<FormattedCharSequence> list = this.font.split(component, 90);
return list.isEmpty() ? FormattedCharSequence.EMPTY : (FormattedCharSequence) list.get(0);
}
);
int drawColor;
boolean drawOutlined;
int drawLight;
@ -109,10 +113,30 @@ public class BaseSignBlockEntityRenderer implements BlockEntityRenderer<BaseSign
FormattedCharSequence formattedCharSequence = formattedCharSequences[s];
float t = (float) (-this.font.width(formattedCharSequence) / 2);
if (drawOutlined) {
this.font.drawInBatch8xOutline(formattedCharSequence, t, (float) (s * 10 - 20), drawColor, m, matrixStack.last().pose(), provider, drawLight);
this.font.drawInBatch8xOutline(
formattedCharSequence,
t,
(float) (s * 10 - 20),
drawColor,
m,
matrixStack.last().pose(),
provider,
drawLight
);
}
else {
this.font.drawInBatch((FormattedCharSequence) formattedCharSequence, t, (float) (s * 10 - 20), drawColor, false, matrixStack.last().pose(), provider, false, 0, drawLight);
this.font.drawInBatch(
(FormattedCharSequence) formattedCharSequence,
t,
(float) (s * 10 - 20),
drawColor,
false,
matrixStack.last().pose(),
provider,
false,
0,
drawLight
);
}
}
@ -160,7 +184,10 @@ public class BaseSignBlockEntityRenderer implements BlockEntityRenderer<BaseSign
public static void registerRenderLayer(Block block) {
ResourceLocation blockId = Registry.BLOCK.getKey(block);
RenderType layer = RenderType.entitySolid(new ResourceLocation(blockId.getNamespace(), "textures/entity/sign/" + blockId.getPath() + ".png"));
RenderType layer = RenderType.entitySolid(new ResourceLocation(
blockId.getNamespace(),
"textures/entity/sign/" + blockId.getPath() + ".png"
));
LAYERS.put(block, layer);
}

View file

@ -4,5 +4,13 @@ import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.SoundType;
public class BlockSounds {
public static final SoundType TERRAIN_SOUND = new SoundType(1.0F, 1.0F, SoundEvents.STONE_BREAK, SoundEvents.WART_BLOCK_STEP, SoundEvents.STONE_PLACE, SoundEvents.STONE_HIT, SoundEvents.STONE_FALL);
public static final SoundType TERRAIN_SOUND = new SoundType(
1.0F,
1.0F,
SoundEvents.STONE_BREAK,
SoundEvents.WART_BLOCK_STEP,
SoundEvents.STONE_PLACE,
SoundEvents.STONE_HIT,
SoundEvents.STONE_FALL
);
}