Skip to content

Commit

Permalink
Properly segregate client-side platform method
Browse files Browse the repository at this point in the history
Fixes #86
  • Loading branch information
62832 committed Nov 2, 2023
1 parent 8dce067 commit ce041ea
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 75 deletions.
20 changes: 12 additions & 8 deletions common/src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ private MEGACells() {}

public static final Platform PLATFORM =
ServiceLoader.load(Platform.class).findFirst().orElseThrow();
public static final Platform.Client PLATFORM_CLIENT = PLATFORM.isClient()
? ServiceLoader.load(Platform.Client.class).findFirst().orElseThrow()
: null;

public static ResourceLocation makeId(String path) {
return new ResourceLocation(MODID, path);
Expand All @@ -56,7 +59,14 @@ public static void initCommon() {
AppBotItems.init();
}

initStorageCells();
StorageCells.addCellHandler(BulkCellItem.HANDLER);

MEGAItems.getItemPortables()
.forEach(cell -> HotkeyActions.registerPortableCell(cell, HotkeyAction.PORTABLE_ITEM_CELL));
MEGAItems.getFluidPortables()
.forEach(cell -> HotkeyActions.registerPortableCell(cell, HotkeyAction.PORTABLE_FLUID_CELL));

initStorageCellModels();

PLATFORM.initCompression();
GridServices.register(DecompressionService.class, DecompressionService.class);
Expand All @@ -67,22 +77,16 @@ public static void initCommon() {
PLATFORM.addVillagerTrade(MEGAItems.ACCUMULATION_PROCESSOR_PRESS, 40, 1, 50);
}

private static void initStorageCells() {
private static void initStorageCellModels() {
Stream.of(MEGAItems.getItemCells(), MEGAItems.getItemPortables())
.flatMap(Collection::stream)
.forEach(c -> StorageCellModels.registerModel(c, makeId("block/drive/cells/mega_item_cell")));
Stream.of(MEGAItems.getFluidCells(), MEGAItems.getFluidPortables())
.flatMap(Collection::stream)
.forEach(c -> StorageCellModels.registerModel(c, makeId("block/drive/cells/mega_fluid_cell")));

StorageCells.addCellHandler(BulkCellItem.HANDLER);
StorageCellModels.registerModel(MEGAItems.BULK_ITEM_CELL, makeId("block/drive/cells/bulk_item_cell"));

MEGAItems.getItemPortables()
.forEach(cell -> HotkeyActions.registerPortableCell(cell, HotkeyAction.PORTABLE_ITEM_CELL));
MEGAItems.getFluidPortables()
.forEach(cell -> HotkeyActions.registerPortableCell(cell, HotkeyAction.PORTABLE_FLUID_CELL));

if (PLATFORM.isAddonLoaded(Addons.APPBOT)) {
Stream.of(AppBotItems.getCells(), AppBotItems.getPortables())
.flatMap(Collection::stream)
Expand Down
6 changes: 5 additions & 1 deletion common/src/main/java/gripe/_90/megacells/core/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
public interface Platform {
Loaders getLoader();

boolean isClient();

CreativeModeTab.Builder getCreativeTabBuilder();

boolean isAddonLoaded(Addons addon);
Expand All @@ -20,5 +22,7 @@ public interface Platform {

void addVillagerTrade(ItemLike item, int cost, int quantity, int xp);

BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation);
interface Client {
BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ private static <T extends AEBaseBlockEntity> BlockEntityType<T> create(
return type;
}

@FunctionalInterface
interface BlockEntityFactory<T extends AEBaseBlockEntity> {
private interface BlockEntityFactory<T extends AEBaseBlockEntity> {
T create(BlockEntityType<T> type, BlockPos pos, BlockState state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public class BulkCellInventory implements StorageCell {
this.container = container;

var cell = (BulkCellItem) stack.getItem();
var filter = cell.getConfigInventory(this.stack).getKey(0);
filterItem = filter instanceof AEItemKey item ? item : null;
filterItem = (AEItemKey) cell.getConfigInventory(this.stack).getKey(0);

storedItem = getTag().contains(KEY) ? AEItemKey.fromTag(getTag().getCompound(KEY)) : null;
unitCount = !getTag().getString(UNIT_COUNT).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import org.jetbrains.annotations.Nullable;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
Expand Down Expand Up @@ -311,11 +313,13 @@ public IPartModel getStaticModels() {
return MODEL;
}

@Environment(EnvType.CLIENT)
@Override
public boolean requireDynamicRender() {
return true;
}

@Environment(EnvType.CLIENT)
@Override
public void renderDynamic(
float partialTicks,
Expand All @@ -341,7 +345,7 @@ public void renderDynamic(
.getModelRenderer()
.tesselateBlock(
getLevel(),
MEGACells.PLATFORM.createWrappedCellModel(clientCell, orientation),
MEGACells.PLATFORM_CLIENT.createWrappedCellModel(clientCell, orientation),
getBlockEntity().getBlockState(),
getBlockEntity().getBlockPos(),
poseStack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@ public static boolean allIngredientsPresent(ItemEntity entity) {
.toList();

for (var recipe : level.getRecipeManager().getAllRecipesFor(TransformRecipe.TYPE)) {
if (!recipe.circumstance.isFluidTag(FluidTags.LAVA)) {
continue;
}

if (recipe.ingredients.isEmpty()) {
continue;
}

return recipe.ingredients.stream().noneMatch(ingredient -> {
for (var stack : ingredient.getItems()) {
if (items.contains(stack.getItem())) {
return false;
if (recipe.circumstance.isFluidTag(FluidTags.LAVA)) {
return recipe.ingredients.stream().noneMatch(ingredient -> {
for (var stack : ingredient.getItems()) {
if (items.contains(stack.getItem())) {
return false;
}
}
}

return true;
});
return true;
});
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper;
Expand Down Expand Up @@ -37,6 +38,11 @@ public Loaders getLoader() {
return Loaders.FABRIC;
}

@Override
public boolean isClient() {
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT;
}

@Override
public CreativeModeTab.Builder getCreativeTabBuilder() {
return FabricItemGroup.builder();
Expand Down Expand Up @@ -72,15 +78,17 @@ public void addVillagerTrade(ItemLike item, int cost, int quantity, int xp) {
builder -> builder.add(new VillagerTrades.ItemsForEmeralds(item.asItem(), cost, quantity, xp)));
}

@Override
public BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation) {
var driveModel = Minecraft.getInstance()
.getModelManager()
.getBlockModelShaper()
.getBlockModel(AEBlocks.DRIVE.block().defaultBlockState());
var cellModel =
BakedModelUnwrapper.unwrap(driveModel, DriveBakedModel.class).getCellChassisModel(cell);
return new WrappedCellModel(cellModel, orientation);
public static class Client implements Platform.Client {
@Override
public BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation) {
var driveModel = Minecraft.getInstance()
.getModelManager()
.getBlockModelShaper()
.getBlockModel(AEBlocks.DRIVE.block().defaultBlockState());
var cellModel = BakedModelUnwrapper.unwrap(driveModel, DriveBakedModel.class)
.getCellChassisModel(cell);
return new WrappedCellModel(cellModel, orientation);
}
}

private static class WrappedCellModel extends ForwardingBakedModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gripe._90.megacells.fabric.FabricPlatform$Client
83 changes: 46 additions & 37 deletions forge/src/main/java/gripe/_90/megacells/forge/ForgePlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.model.data.ModelData;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.OnDatapackSyncEvent;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.loading.LoadingModList;
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;

Expand All @@ -50,6 +52,11 @@ public Loaders getLoader() {
return Loaders.FORGE;
}

@Override
public boolean isClient() {
return FMLEnvironment.dist == Dist.CLIENT;
}

@Override
public CreativeModeTab.Builder getCreativeTabBuilder() {
return CreativeModeTab.builder();
Expand Down Expand Up @@ -102,43 +109,45 @@ public void addVillagerTrade(ItemLike item, int cost, int quantity, int xp) {
offers.put(5, masterEntries);
}

@Override
public BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation) {
var driveModel = Minecraft.getInstance()
.getModelManager()
.getBlockModelShaper()
.getBlockModel(AEBlocks.DRIVE.block().defaultBlockState());
var cellModel =
BakedModelUnwrapper.unwrap(driveModel, DriveBakedModel.class).getCellChassisModel(cell);

return new DelegateBakedModel(cellModel) {
@NotNull
@Override
public List<BakedQuad> getQuads(
@Nullable BlockState state,
@Nullable Direction side,
@NotNull RandomSource rand,
@NotNull ModelData extraData,
RenderType renderType) {
if (side != null) {
side = orientation.resultingRotate(side); // This fixes the incorrect lightmap position
public static class Client implements Platform.Client {
@Override
public BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation) {
var driveModel = Minecraft.getInstance()
.getModelManager()
.getBlockModelShaper()
.getBlockModel(AEBlocks.DRIVE.block().defaultBlockState());
var cellModel = BakedModelUnwrapper.unwrap(driveModel, DriveBakedModel.class)
.getCellChassisModel(cell);

return new DelegateBakedModel(cellModel) {
@NotNull
@Override
public List<BakedQuad> getQuads(
@Nullable BlockState state,
@Nullable Direction side,
@NotNull RandomSource rand,
@NotNull ModelData extraData,
RenderType renderType) {
if (side != null) {
side = orientation.resultingRotate(side); // This fixes the incorrect lightmap position
}
List<BakedQuad> quads = new ArrayList<>(super.getQuads(state, side, rand, extraData, renderType));

for (int i = 0; i < quads.size(); i++) {
BakedQuad quad = quads.get(i);
quads.set(
i,
new BakedQuad(
quad.getVertices(),
quad.getTintIndex(),
orientation.rotate(quad.getDirection()),
quad.getSprite(),
quad.isShade()));
}

return quads;
}
List<BakedQuad> quads = new ArrayList<>(super.getQuads(state, side, rand, extraData, renderType));

for (int i = 0; i < quads.size(); i++) {
BakedQuad quad = quads.get(i);
quads.set(
i,
new BakedQuad(
quad.getVertices(),
quad.getTintIndex(),
orientation.rotate(quad.getDirection()),
quad.getSprite(),
quad.isShade()));
}

return quads;
}
};
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gripe._90.megacells.forge.ForgePlatform$Client

0 comments on commit ce041ea

Please sign in to comment.