Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeBather committed Nov 22, 2024
2 parents 22e3054 + d68b5e4 commit fa4498d
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/kasuga/lib/core/KasugaLibStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import kasuga.lib.core.menu.targets.TargetsClient;
import kasuga.lib.core.util.Envs;
import kasuga.lib.registrations.client.KeyBindingReg;
import kasuga.lib.registrations.common.FluidReg;
import kasuga.lib.registrations.registry.SimpleRegistry;
import kasuga.lib.registrations.registry.FontRegistry;
import kasuga.lib.registrations.registry.TextureRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
import net.minecraft.util.RandomSource;
Expand All @@ -53,6 +55,7 @@ public class KasugaLibStacks {
private final FontRegistry FONTS;
private final RandomSource random = RandomSource.create();
private final HashMap<Block, CustomBlockRenderer> BLOCK_RENDERERS;
public static final HashMap<FluidReg<?>, RenderType> FLUID_RENDERS = new HashMap<>();

public final JavascriptApi JAVASCRIPT = new JavascriptApi();

Expand Down Expand Up @@ -111,6 +114,7 @@ public KasugaLibStacks(IEventBus bus) {
bus.addListener(AnimationModelRegistryEvent::registerAnimations);
if (Envs.isDevEnvironment()) KasugaLibClient.invoke();
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()-> TargetsClient::register);
bus.addListener(REGISTRY::hookFluidAndRenders);
}

MinecraftForge.EVENT_BUS.addListener(ServerResourceListener::onServerStarting);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/kasuga/lib/core/network/PacketPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kasuga.lib.core.network;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkConstants;
import net.minecraftforge.network.NetworkDirection;

public abstract class PacketPair {

public PacketPair() {}
public abstract void encode(FriendlyByteBuf buf);
public abstract void decode(FriendlyByteBuf buf);
}
17 changes: 16 additions & 1 deletion src/main/java/kasuga/lib/example_env/AllExampleElements.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kasuga.lib.example_env;

import com.mojang.math.Vector3f;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.base.BucketItem;
import kasuga.lib.core.menu.base.GuiBinding;
Expand All @@ -23,12 +24,19 @@
import kasuga.lib.example_env.network.ExampleS2CPacket;
import kasuga.lib.registrations.common.*;
import kasuga.lib.registrations.registry.SimpleRegistry;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraftforge.common.SoundAction;
import net.minecraftforge.common.SoundActions;

public class AllExampleElements {

Expand Down Expand Up @@ -127,7 +135,14 @@ public class AllExampleElements {
.numericProperties(1, 8, 3, 10)
.overlayTexPath("block/fluid/water_overlay")
.bucketItem(BucketItem::new)
.blockType(ExampleFluidBlock::new)
.basicFluidProperties(5, 15, 5, true)
.defaultSounds()
.tintColor(0xFFD2691E)
.fogColor(210, 105, 30)
.blockType((fluid, properties) ->
new ExampleFluidBlock(fluid, BlockBehaviour.Properties.copy(Blocks.WATER)))
.noLootAndOcclusion()
.setRenderType(RenderType.translucent())
.tab(tab)
.submit(REGISTRY);

Expand Down
131 changes: 129 additions & 2 deletions src/main/java/kasuga/lib/registrations/common/FluidReg.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package kasuga.lib.registrations.common;

import com.mojang.blaze3d.shaders.FogShape;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.math.Vector3f;
import kasuga.lib.core.annos.Inner;
import kasuga.lib.core.annos.Mandatory;
import kasuga.lib.core.annos.Optional;
import kasuga.lib.registrations.Reg;
import kasuga.lib.registrations.registry.SimpleRegistry;
import net.minecraft.client.Camera;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;
import net.minecraftforge.common.SoundAction;
import net.minecraftforge.common.SoundActions;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.network.IContainerFactory;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -50,6 +63,8 @@ public class FluidReg<E extends ForgeFlowingFluid> extends Reg {
private int tintColor = 0xffffff;
boolean registerItem = false, registerBlock = false, registerMenu = false;
private final FluidTagReg tag;
private RenderType renderType = null;
private Vector3f fogColor = null;

/**
* Create a fluid registration.
Expand Down Expand Up @@ -307,6 +322,26 @@ public FluidReg<E> withBlockProperty(BlockReg.PropertyIdentifier identifier) {
return this;
}

@Optional
public FluidReg<E> noOcclusion() {
return withBlockProperty(BlockBehaviour.Properties::noOcclusion);
}

@Optional
public FluidReg<E> noCollision() {
return withBlockProperty(BlockBehaviour.Properties::noCollission);
}

@Optional
public FluidReg<E> noLoot() {
return withBlockProperty(BlockBehaviour.Properties::noLootTable);
}

@Optional
public FluidReg<E> noLootAndOcclusion() {
return noLoot().noOcclusion();
}

/**
* What color your fluid texture would be, 0xffffff(white) in default.
* @param r red.
Expand Down Expand Up @@ -353,6 +388,82 @@ public FluidReg<E> typeProperty(PropertyBuilder builder) {
return this;
}

@Mandatory
public FluidReg<E> basicFluidProperties(int lightLevel, int density, int viscosity, boolean canSupportBoating) {
return lightLevel(lightLevel).canSupportBoating(canSupportBoating).density(density).viscosity(viscosity);

}

public FluidReg<E> lightLevel(int lightLevel) {
return this.typeProperty(prop -> prop.lightLevel(lightLevel));
}

public FluidReg<E> canSupportBoating(boolean flag) {
return this.typeProperty(prop -> prop.supportsBoating(flag));
}

public FluidReg<E> density(int density) {
return this.typeProperty(prop -> prop.density(density));
}

public FluidReg<E> viscosity(int viscosity) {
return this.typeProperty(prop -> prop.viscosity(viscosity));
}

@Optional
public FluidReg<E> bucketFillSound(SoundEvent sound) {
return this.typeProperty(prop -> prop.sound(SoundActions.BUCKET_FILL, sound));
}

@Optional
public FluidReg<E> bucketEmptySound(SoundEvent sound) {
return this.typeProperty(prop -> prop.sound(SoundActions.BUCKET_EMPTY, sound));
}

@Optional
public FluidReg<E> vaporizeSound(SoundEvent sound) {
return this.typeProperty(prop -> prop.sound(SoundActions.FLUID_VAPORIZE, sound));
}

@Optional
public FluidReg<E> sound(SoundAction action, SoundEvent sound) {
return this.typeProperty(prop -> prop.sound(action, sound));
}

@Optional
public FluidReg<E> sound(SoundEvent fillSound, SoundEvent emptySound, SoundEvent vaporizeSound) {
return bucketFillSound(fillSound).bucketEmptySound(emptySound).vaporizeSound(vaporizeSound);
}

@Optional
public FluidReg<E> defaultSounds() {
return sound(SoundEvents.BUCKET_FILL, SoundEvents.BUCKET_EMPTY, SoundEvents.FIRE_EXTINGUISH);
}

@Optional
public FluidReg<E> setRenderType(RenderType type) {
this.renderType = type;
return this;
}

@Optional
public FluidReg<E> setTranslucentRenderType() {
this.renderType = RenderType.translucent();
return this;
}

@Optional
public FluidReg<E> fogColor(Vector3f fogColor) {
this.fogColor = fogColor;
return this;
}

@Optional
public FluidReg<E> fogColor(float r, float g, float b) {
this.fogColor = new Vector3f(r / 255f, g / 255f, b / 255f);
return this;
}

/**
* Customize your fluid's property.
* @param builder your fluid's property customizer lambda.
Expand All @@ -373,7 +484,7 @@ public FluidReg<E> fluidProperty(FluidPropertyBuilder builder) {
@Override
public FluidReg<E> submit(SimpleRegistry registry) {
properties.descriptionId(registrationKey);

registry.cacheFluidRenderIn(this);
propertyBuilders.forEach(b -> b.build(properties));
if (flowingBuilder == null) {
crashOnNotPresent(ForgeFlowingFluid.class, "flow", "submit");
Expand All @@ -389,7 +500,7 @@ public FluidReg<E> submit(SimpleRegistry registry) {
for(FluidPropertyBuilder builder : builders)
builder.build(fluidProp);
if(stillBuilder != null)
stillObject = registry.fluid().register(registrationKey + "_still", () -> stillBuilder.build(fluidProp));
stillObject = registry.fluid().register(registrationKey, () -> stillBuilder.build(fluidProp));
if(flowingBuilder != null)
flowingObject = registry.fluid().register(registrationKey + "_flow", () -> flowingBuilder.build(fluidProp));
if (registerItem) itemReg.submit(registry);
Expand Down Expand Up @@ -433,6 +544,10 @@ public LiquidBlock legacyBlock() {
return block.getBlock();
}

public RenderType getRenderType() {
return renderType;
}

@Override
public String getIdentifier() {
return "fluid";
Expand All @@ -445,6 +560,7 @@ private FluidType initDefaultType(SimpleRegistry registry) {
ResourceLocation overlayLoc = overlayTexturePath == null ? null : new ResourceLocation(registry.namespace, overlayTexturePath);

FluidType type = new FluidType(properties) {

@Override
public void initializeClient(Consumer<IClientFluidTypeExtensions> consumer) {
consumer.accept(
Expand Down Expand Up @@ -474,6 +590,17 @@ public ResourceLocation getFlowingTexture() {
return overlayLoc;
return IClientFluidTypeExtensions.super.getOverlayTexture();
}

@Override
public @NotNull Vector3f modifyFogColor(Camera camera, float partialTick, ClientLevel level, int renderDistance, float darkenWorldAmount, Vector3f fluidFogColor) {
return fogColor;
}

@Override
public void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float renderDistance, float partialTick, float nearDistance, float farDistance, FogShape shape) {
RenderSystem.setShaderFogStart(1f);
RenderSystem.setShaderFogEnd(6f);
}
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import kasuga.lib.registrations.client.AnimReg;
import kasuga.lib.registrations.client.KeyBindingReg;
import kasuga.lib.registrations.common.*;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
Expand All @@ -30,8 +32,13 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
Expand Down Expand Up @@ -351,6 +358,12 @@ public boolean hasMenuCache(String registrationKey) {
return CACHE_OF_MENUS.getOrDefault(registrationKey, null);
}

@Inner
public void cacheFluidRenderIn(FluidReg<?> reg) {
if (reg.getRenderType() == null) return;
KasugaLibStacks.FLUID_RENDERS.put(reg, reg.getRenderType());
}

public HashMap<String, MenuReg<?, ?>> getCahcedMenus() {
return CACHE_OF_MENUS;
}
Expand Down Expand Up @@ -426,6 +439,16 @@ public void onCustomItemRendererReg(Map<ResourceLocation, BakedModel> registry)
CUSTOM_RENDERED_ITEMS.clear();
}

@Inner
@SubscribeEvent
@OnlyIn(Dist.CLIENT)
public void hookFluidAndRenders(ModelEvent.RegisterAdditional event) {
for (Map.Entry<FluidReg<?>, RenderType> entry : KasugaLibStacks.FLUID_RENDERS.entrySet()) {
ItemBlockRenderTypes.setRenderLayer(entry.getKey().stillFluid(), entry.getValue());
ItemBlockRenderTypes.setRenderLayer(entry.getKey().flowingFluid(), entry.getValue());
}
}

public void onAnimationReg() {
ANIMS.forEach((k, v) -> v.onResourceLoading());
}
Expand Down

0 comments on commit fa4498d

Please sign in to comment.