Code cleanup and refactoring before porting

This commit is contained in:
2023-05-10 21:21:16 +02:00
parent 1dec8d130c
commit 8e72212bf6
134 changed files with 975 additions and 755 deletions

View File

@@ -0,0 +1,12 @@
package com.hypherionmc.craterlib.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
public class CraterLibClientInitializer implements ClientModInitializer {
@Override
public void onInitializeClient() {
ItemGroupEvents.MODIFY_ENTRIES_ALL.register(FabricClientHelper::registerCreativeItems);
}
}

View File

@@ -0,0 +1,101 @@
package com.hypherionmc.craterlib.client;
import com.hypherionmc.craterlib.api.rendering.CustomRenderType;
import com.hypherionmc.craterlib.common.item.BlockItemDyable;
import com.hypherionmc.craterlib.core.network.CraterPacket;
import com.hypherionmc.craterlib.core.platform.services.LibClientHelper;
import com.hypherionmc.craterlib.util.ColorPropertyFunction;
import me.hypherionmc.craterlib.systems.reg.RegistryObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroupEntries;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.object.builder.v1.client.model.FabricModelPredicateProviderRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.network.Connection;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import java.util.Collection;
import java.util.function.Function;
/**
* @author HypherionSA
*/
public class FabricClientHelper implements LibClientHelper {
@Override
public void registerItemProperty(BlockItemDyable item, String property) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
FabricModelPredicateProviderRegistry.register(item, new ResourceLocation(property), new ColorPropertyFunction(item));
}
}
@Override
public void registerCustomRenderTypes(Collection<RegistryObject<Block>> blocks, Collection<RegistryObject<Item>> items) {
blocks.forEach(blk -> {
if (blk.get() instanceof CustomRenderType type) {
BlockRenderLayerMap.INSTANCE.putBlock(blk.get(), type.getCustomRenderType());
}
});
items.forEach(itm -> {
if (itm.get() instanceof BlockItemDyable dyable && dyable.getBlock() instanceof CustomRenderType customRenderType) {
BlockRenderLayerMap.INSTANCE.putItem(itm.get(), customRenderType.getCustomRenderType());
}
if (itm.get() instanceof CustomRenderType customRenderType) {
BlockRenderLayerMap.INSTANCE.putItem(itm.get(), customRenderType.getCustomRenderType());
}
});
}
@Override
public Minecraft getClientInstance() {
return Minecraft.getInstance();
}
@Override
public Player getClientPlayer() {
return Minecraft.getInstance().player;
}
@Override
public Level getClientLevel() {
return Minecraft.getInstance().level;
}
@Override
public Connection getClientConnection() {
return Minecraft.getInstance().getConnection().getConnection();
}
@Override
public void registerClientReceiver(ResourceLocation channelName, Function<FriendlyByteBuf, CraterPacket<?>> factory) {
ClientPlayNetworking.registerGlobalReceiver(channelName, (Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) -> {
CraterPacket<?> packet = factory.apply(buf);
client.execute(() -> packet.handle(client.player, client));
});
}
public static void registerCreativeItems(CreativeModeTab tab, FabricItemGroupEntries entries) {
}
@Override
public void registerBlockEntityRenderer(BlockEntityType<? extends BlockEntity> blockEntityType, BlockEntityRendererProvider blockEntityRendererFactory) {
BlockEntityRendererRegistry.register(blockEntityType, blockEntityRendererFactory);
}
}

View File

@@ -0,0 +1,94 @@
package com.hypherionmc.craterlib.client.gui.widgets;
import com.hypherionmc.craterlib.systems.fluid.FluidTank;
import com.hypherionmc.craterlib.util.RenderUtils;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
/**
* Modified from <a href="https://github.com/SleepyTrousers/EnderIO-Rewrite/blob/dev/1.18.x/enderio-machines/src/main/java/com/enderio/machines/client/FluidStackWidget.java">...</a>
*/
public class FluidStackWidget extends AbstractWidget {
private final Screen displayOn;
private final Supplier<FluidTank> getFluid;
private final String toolTipTitle;
public FluidStackWidget(Screen displayOn, Supplier<FluidTank> getFluid, int pX, int pY, int pWidth, int pHeight, String tooltipTitle) {
super(pX, pY, pWidth, pHeight, Component.empty());
this.displayOn = displayOn;
this.getFluid = getFluid;
this.toolTipTitle = tooltipTitle;
}
@Override
public void renderWidget(@NotNull PoseStack matrices, int mouseX, int mouseY, float delta) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
FluidTank fluidTank = getFluid.get();
if (!fluidTank.getResource().isBlank()) {
FluidVariant fluidStack = fluidTank.getResource();
TextureAtlasSprite still = FluidVariantRendering.getSprite(fluidStack);
if (still != null) {
RenderSystem.setShaderTexture(0, TextureAtlas.LOCATION_BLOCKS);
int color = FluidVariantRendering.getColor(fluidStack);
RenderSystem.setShaderColor(
RenderUtils.ARGB32.red(color) / 255.0F,
RenderUtils.ARGB32.green(color) / 255.0F,
RenderUtils.ARGB32.blue(color) / 255.0F,
RenderUtils.ARGB32.alpha(color) / 255.0F);
RenderSystem.enableBlend();
long stored = fluidTank.getAmount();
float capacity = fluidTank.getCapacity();
float filledVolume = stored / capacity;
int renderableHeight = (int) (filledVolume * height);
int atlasWidth = (int) (still.getX() / (still.getU1() - still.getU0()));
int atlasHeight = (int) (still.getY() / (still.getV1() - still.getV0()));
matrices.pushPose();
matrices.translate(0, height - 16, 0);
for (int i = 0; i < Math.ceil(renderableHeight / 16f); i++) {
int drawingHeight = Math.min(16, renderableHeight - 16 * i);
int notDrawingHeight = 16 - drawingHeight;
// TODO Double Check this
blit(matrices, getX(), getY() + notDrawingHeight, 0, still.getU0() * atlasWidth, still.getV0() * atlasHeight + notDrawingHeight, this.width, drawingHeight, atlasWidth, atlasHeight);
matrices.translate(0, -16, 0);
}
RenderSystem.setShaderColor(1, 1, 1, 1);
matrices.popPose();
}
//renderToolTip(matrices, mouseX, mouseY);
}
}
// TODO Fix Tooltips
/*@Override
public void renderToolTip(PoseStack poseStack, int mouseX, int mouseY) {
if (this.visible && this.isFocused() && isHoveredOrFocused()) {
displayOn.renderTooltip(poseStack, Arrays.asList(LangUtils.getTooltipTitle(toolTipTitle), Component.literal((int) (((float) this.getFluid.get().getAmount() / this.getFluid.get().getCapacity()) * 100) + "%")), Optional.empty(), mouseX, mouseY);
}
}*/
@Override
protected void updateWidgetNarration(NarrationElementOutput narrationElementOutput) {
}
}