Skip to content

Commit

Permalink
Rewrite platform bridges in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Aug 2, 2024
1 parent 67d7d51 commit 1daeed5
Show file tree
Hide file tree
Showing 42 changed files with 205 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public final class AdornBlockEntities {
public static final Registered<BlockEntityType<KitchenCupboardBlockEntity>> KITCHEN_CUPBOARD =
register("kitchen_cupboard", KitchenCupboardBlockEntity::new, KitchenCupboardBlock.class);
public static final Registered<BlockEntityType<KitchenSinkBlockEntity>> KITCHEN_SINK =
register("kitchen_sink", PlatformBridges.Companion.getBlockEntities()::createKitchenSink, KitchenSinkBlock.class);
register("kitchen_sink", PlatformBridges.get().getBlockEntities()::createKitchenSink, KitchenSinkBlock.class);
public static final Registered<BlockEntityType<TradingStationBlockEntity>> TRADING_STATION =
register("trading_station", TradingStationBlockEntity::new, AdornBlocks.TRADING_STATION);
public static final Registered<BlockEntityType<BrewerBlockEntity>> BREWER =
register("brewer", PlatformBridges.Companion.getBlockEntities()::createBrewer, AdornBlocks.BREWER);
register("brewer", PlatformBridges.get().getBlockEntities()::createBrewer, AdornBlocks.BREWER);

private static <E extends BlockEntity> Registered<BlockEntityType<E>> register(String name, BlockEntityFactory<E> factory, Supplier<? extends Block> block) {
return BLOCK_ENTITIES.register(name, () -> BlockEntityType.Builder.create(factory, block.get()).build(null));
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/juuxel/adorn/block/AdornBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class AdornBlocks {
DyeColor.values(),
color -> HELPER.registerBlock(
color.asString() + "_sofa",
() -> PlatformBridges.Companion.getBlockFactory().createSofa(BlockVariant.wool(color))
() -> PlatformBridges.get().getBlockFactory().createSofa(BlockVariant.wool(color))
)
);

Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/juuxel/adorn/block/DrawerBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
if (world.isClient) return ActionResult.SUCCESS;

if (world.getBlockEntity(pos) instanceof DrawerBlockEntity drawer) {
PlatformBridges.Companion.getMenus().open(player, drawer, pos);;
PlatformBridges.get().getMenus().open(player, drawer, pos);;
player.incrementStat(AdornStats.OPEN_DRAWER);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
if (world.isClient) return ActionResult.SUCCESS;

if (world.getBlockEntity(pos) instanceof KitchenCupboardBlockEntity cupboard) {
PlatformBridges.Companion.getMenus().open(player, cupboard, pos);
PlatformBridges.get().getMenus().open(player, cupboard, pos);
player.incrementStat(AdornStats.OPEN_KITCHEN_CUPBOARD);
}

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/juuxel/adorn/block/ShelfBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
inventory.setStack(slot, stack);
be.markDirty();
if (!world.isClient) {
PlatformBridges.Companion.getNetwork().syncBlockEntity(be);
PlatformBridges.get().getNetwork().syncBlockEntity(be);
player.incrementStat(AdornStats.INTERACT_WITH_SHELF);
}

Expand All @@ -138,7 +138,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
inventory.setStack(slot, ItemStack.EMPTY);
be.markDirty();
if (!world.isClient) {
PlatformBridges.Companion.getNetwork().syncBlockEntity(be);
PlatformBridges.get().getNetwork().syncBlockEntity(be);
player.incrementStat(AdornStats.INTERACT_WITH_SHELF);
}
}
Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/juuxel/adorn/client/ClientNetworkBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package juuxel.adorn.client;

import juuxel.adorn.util.InlineServices;
import juuxel.adorn.util.Services;
import net.minecraft.item.ItemStack;

@InlineServices
public interface ClientNetworkBridge {
void sendSetTradeStack(int syncId, int slotId, ItemStack stack);

@InlineServices.Getter
static ClientNetworkBridge get() {
return Services.load(ClientNetworkBridge.class);
}
}
33 changes: 33 additions & 0 deletions common/src/main/java/juuxel/adorn/client/FluidRenderingBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package juuxel.adorn.client;

import juuxel.adorn.fluid.FluidReference;
import juuxel.adorn.util.InlineServices;
import juuxel.adorn.util.Services;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.client.texture.Sprite;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@InlineServices
public interface FluidRenderingBridge {
@Nullable Sprite getStillSprite(FluidReference volume);

int getColor(FluidReference volume, @Nullable BlockRenderView world, @Nullable BlockPos pos);

default int getColor(FluidReference volume) {
return getColor(volume, null, null);
}

boolean fillsFromTop(FluidReference volume);

List<Text> getTooltip(FluidReference volume, TooltipContext context, @Nullable Integer maxAmountInLitres);

@InlineServices.Getter
static FluidRenderingBridge get() {
return Services.load(FluidRenderingBridge.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PalettedMenuScreen(M menu, PlayerInventory playerInventory, Text title) {
protected abstract Identifier getPaletteId();

private ColorManager.ColorPair getPalette() {
return PlatformBridges.Companion.getResources().getColorManager().getColors(getPaletteId()).get(blockId);
return PlatformBridges.get().getResources().getColorManager().getColors(getPaletteId()).get(blockId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public final class AdornEntities {
public static final Registrar<EntityType<?>> ENTITIES = RegistrarFactory.get().create(RegistryKeys.ENTITY_TYPE);

public static final Registered<EntityType<SeatEntity>> SEAT =
ENTITIES.register("seat", PlatformBridges.Companion.getEntities()::createSeatType);
ENTITIES.register("seat", PlatformBridges.get().getEntities()::createSeatType);

public static void init() {
}
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/juuxel/adorn/entity/SeatEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void removePassenger(Entity passenger) {
public void kill() {
removeAllPassengers();
if (!getWorld().isClient) {
PlatformBridges.Companion.getNetwork().sendToTracking(this, new EntityPassengersSetS2CPacket(this));
PlatformBridges.get().getNetwork().sendToTracking(this, new EntityPassengersSetS2CPacket(this));
}
super.kill();
var state = getWorld().getBlockState(seatPos);
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/juuxel/adorn/item/AdornBookItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AdornBookItem(Identifier bookId, Settings settings) {
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (!world.isClient) {
PlatformBridges.Companion.getNetwork().sendOpenBookPacket(user, bookId);
PlatformBridges.get().getNetwork().sendOpenBookPacket(user, bookId);
}
user.incrementStat(Stats.USED.getOrCreateStat(this));

Expand All @@ -37,7 +37,7 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
var bookManager = PlatformBridges.Companion.getResources().getBookManager();
var bookManager = PlatformBridges.get().getResources().getBookManager();
if (bookManager.contains(bookId)) {
tooltip.add(Text.translatable("book.byAuthor", bookManager.get(bookId).author()).formatted(Formatting.GRAY));
}
Expand Down
8 changes: 3 additions & 5 deletions common/src/main/java/juuxel/adorn/menu/AdornMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import juuxel.adorn.platform.MenuBridge;
import juuxel.adorn.platform.PlatformBridges;
import kotlin.jvm.functions.Function3;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.menu.Menu;
import net.minecraft.menu.MenuType;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.resource.featuretoggle.FeatureFlags;

Expand All @@ -26,7 +24,7 @@ public final class AdornMenus {
public static void init() {
}

private static <M extends Menu> MenuType<M> createType(Function3<Integer, PlayerInventory, PacketByteBuf, M> factory) {
return PlatformBridges.Companion.getMenus().createType(factory);
private static <M extends Menu> MenuType<M> createType(MenuBridge.Factory<M> factory) {
return PlatformBridges.get().getMenus().createType(factory);
}
}
2 changes: 1 addition & 1 deletion common/src/main/java/juuxel/adorn/menu/BrewerMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void sendContentUpdates() {
var last = lastFluid;
if (last == null || !FluidReference.areFluidsAndAmountsEqual(fluid, last)) {
lastFluid = fluid.createSnapshot();
PlatformBridges.Companion.getNetwork().sendBrewerFluidSync(player, syncId, fluid);
PlatformBridges.get().getNetwork().sendBrewerFluidSync(player, syncId, fluid);
}
}

Expand Down
11 changes: 11 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/BlockEntityBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package juuxel.adorn.platform;

import juuxel.adorn.block.entity.BrewerBlockEntity;
import juuxel.adorn.block.entity.KitchenSinkBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;

public interface BlockEntityBridge {
BrewerBlockEntity createBrewer(BlockPos pos, BlockState state);
KitchenSinkBlockEntity createKitchenSink(BlockPos pos, BlockState state);
}
12 changes: 12 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/BlockFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package juuxel.adorn.platform;

import juuxel.adorn.block.SofaBlock;
import juuxel.adorn.block.variant.BlockVariant;

public interface BlockFactory {
BlockFactory DEFAULT = new BlockFactory() {};

default SofaBlock createSofa(BlockVariant variant) {
return new SofaBlock(variant);
}
}
8 changes: 8 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/EntityBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package juuxel.adorn.platform;

import juuxel.adorn.entity.SeatEntity;
import net.minecraft.entity.EntityType;

public interface EntityBridge {
EntityType<SeatEntity> createSeatType();
}
25 changes: 25 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/FluidBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package juuxel.adorn.platform;

import juuxel.adorn.fluid.FluidAmountPredicate;
import juuxel.adorn.fluid.FluidUnit;
import juuxel.adorn.fluid.FluidVolume;
import juuxel.adorn.util.InlineServices;
import juuxel.adorn.util.Services;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.Fluid;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

@InlineServices
public interface FluidBridge {
FluidUnit getFluidUnit();

@Nullable FluidVolume drain(World world, BlockPos pos, @Nullable BlockState state, Direction side, Fluid fluid, FluidAmountPredicate amountPredicate);

@InlineServices.Getter
static FluidBridge get() {
return Services.load(FluidBridge.class);
}
}
25 changes: 25 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/MenuBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package juuxel.adorn.platform;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.menu.Menu;
import net.minecraft.menu.MenuType;
import net.minecraft.menu.NamedMenuFactory;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

public interface MenuBridge {
/**
* Opens a menu with a pos with the opening NBT sent to the client.
* Does nothing on the client.
*/
void open(PlayerEntity player, @Nullable NamedMenuFactory factory, BlockPos pos);

<M extends Menu> MenuType<M> createType(Factory<M> factory);

@FunctionalInterface
interface Factory<M extends Menu> {
M create(int syncId, PlayerInventory inventory, PacketByteBuf buf);
}
}
22 changes: 22 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/NetworkBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package juuxel.adorn.platform;

import juuxel.adorn.fluid.FluidReference;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.Packet;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;

public interface NetworkBridge {
void sendToTracking(Entity entity, Packet<?> packet);
void sendOpenBookPacket(PlayerEntity player, Identifier bookId);
void sendBrewerFluidSync(PlayerEntity player, int syncId, FluidReference fluid);

default void syncBlockEntity(BlockEntity be) {
if (!(be.getWorld() instanceof ServerWorld world)) {
throw new IllegalStateException("[Adorn] Block entities cannot be synced client->server");
}
world.getChunkManager().markForUpdate(be.getPos());
}
}
19 changes: 19 additions & 0 deletions common/src/main/java/juuxel/adorn/platform/PlatformBridges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package juuxel.adorn.platform;

import juuxel.adorn.util.InlineServices;
import juuxel.adorn.util.Services;

@InlineServices
public interface PlatformBridges {
BlockEntityBridge getBlockEntities();
BlockFactory getBlockFactory();
EntityBridge getEntities();
MenuBridge getMenus();
NetworkBridge getNetwork();
ResourceBridge getResources();

@InlineServices.Getter
static PlatformBridges get() {
return Services.load(PlatformBridges.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package juuxel.adorn.platform;

import juuxel.adorn.client.book.BookManager;
import juuxel.adorn.client.resources.ColorManager;

public interface ResourceBridge {
BookManager getBookManager();
ColorManager getColorManager();
}
17 changes: 0 additions & 17 deletions common/src/main/kotlin/juuxel/adorn/client/ClientNetworkBridge.kt

This file was deleted.

This file was deleted.

26 changes: 0 additions & 26 deletions common/src/main/kotlin/juuxel/adorn/client/FluidRenderingBridge.kt

This file was deleted.

11 changes: 0 additions & 11 deletions common/src/main/kotlin/juuxel/adorn/platform/BlockEntityBridge.kt

This file was deleted.

Loading

0 comments on commit 1daeed5

Please sign in to comment.