Skip to content

Commit

Permalink
Port to Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Aug 11, 2024
1 parent 6cc6782 commit 6b0b9b8
Show file tree
Hide file tree
Showing 18 changed files with 731 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ org.gradle.daemon=false
org.gradle.caching=true

# Dependencies
cyclopscore_version=1.19.9-542
cyclopscore_version=1.19.9-545
4 changes: 4 additions & 0 deletions loader-common/src/main/resources/flopper.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessWidener v1 named

# ArgumentTypeInfos
accessible field net/minecraft/world/level/block/LiquidBlock fluid Lnet/minecraft/world/level/material/FlowingFluid;
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
import net.fabricmc.api.ModInitializer;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import org.cyclops.cyclopscore.Reference;
import org.cyclops.cyclopscore.config.ConfigHandler;
import org.cyclops.cyclopscore.init.ModBaseFabric;
import org.cyclops.cyclopscore.proxy.IClientProxyCommon;
import org.cyclops.cyclopscore.proxy.ICommonProxyCommon;
import org.cyclops.flopper.block.BlockFlopperConfig;
import org.cyclops.flopper.blockentity.BlockEntityFlopperConfig;
import org.cyclops.flopper.block.BlockFlopperConfigFabric;
import org.cyclops.flopper.blockentity.BlockEntityFlopperConfigFabric;
import org.cyclops.flopper.proxy.ClientProxyFabric;
import org.cyclops.flopper.proxy.CommonProxyFabric;

/**
* The main mod class of Flopper.
* @author rubensworks
*/
public class FlopperMainFabric extends ModBaseFabric<FlopperMainFabric> implements ModInitializer {
public class FlopperFabric extends ModBaseFabric<FlopperFabric> implements ModInitializer {

/**
* The unique instance of this mod.
*/
public static FlopperMainFabric _instance;
public static FlopperFabric _instance;

public FlopperMainFabric() {
public FlopperFabric() {
super(Reference.MOD_ID, (instance) -> _instance = instance);
}

Expand Down Expand Up @@ -53,7 +52,7 @@ protected CreativeModeTab.Builder constructDefaultCreativeModeTab(CreativeModeTa
protected void onConfigsRegister(ConfigHandler configHandler) {
super.onConfigsRegister(configHandler);

configHandler.addConfigurable(new BlockFlopperConfig<>(this, null));
configHandler.addConfigurable(new BlockEntityFlopperConfig<>(this, null));
configHandler.addConfigurable(new BlockFlopperConfigFabric());
configHandler.addConfigurable(new BlockEntityFlopperConfigFabric());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.cyclops.flopper.block;

import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.material.MapColor;
import org.cyclops.cyclopscore.config.ConfigurablePropertyCommon;
import org.cyclops.cyclopscore.config.ModConfigLocation;
import org.cyclops.cyclopscore.init.ModBaseFabric;
import org.cyclops.flopper.FlopperFabric;
import org.cyclops.flopper.blockentity.BlockEntityFlopperFabric;

/**
* Config for the {@link BlockFlopper}.
* @author rubensworks
*
*/
public class BlockFlopperConfigFabric extends BlockFlopperConfig<ModBaseFabric<?>> {

@ConfigurablePropertyCommon(category = "machine", comment = "The maximum capacity in mB.", isCommandable = true, requiresMcRestart = true, configLocation = ModConfigLocation.SERVER)
public static long capacityDroplets = 5 * FluidConstants.BUCKET;

@ConfigurablePropertyCommon(category = "machine", comment = "The rate at which fluids can be pulled from other tanks.", isCommandable = true, configLocation = ModConfigLocation.SERVER)
public static long pullFluidRateDroplets = FluidConstants.BUCKET / 10;

@ConfigurablePropertyCommon(category = "machine", comment = "The rate at which fluids can be pushed to other tanks.", isCommandable = true, configLocation = ModConfigLocation.SERVER)
public static long pushFluidRateDroplets = FluidConstants.BUCKET / 10;

public BlockFlopperConfigFabric() {
super(
FlopperFabric._instance,
eConfig -> new BlockFlopperFabric(Block.Properties.of()
.mapColor(MapColor.STONE)
.strength(3.0F, 4.8F)
.sound(SoundType.METAL), BlockEntityFlopperFabric::new)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.cyclops.flopper.block;

import com.mojang.serialization.MapCodec;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.cyclops.cyclopscore.blockentity.CyclopsBlockEntityCommon;
import org.cyclops.cyclopscore.helper.IFluidHelpersFabric;
import org.cyclops.flopper.FlopperFabric;
import org.cyclops.flopper.blockentity.BlockEntityFlopperFabric;

import java.util.function.BiFunction;

/**
* @author rubensworks
*/
public class BlockFlopperFabric extends BlockFlopper {
public static final MapCodec<BlockFlopper> CODEC = BlockBehaviour.simpleCodec(properties -> new BlockFlopperFabric(properties, BlockEntityFlopperFabric::new));

public BlockFlopperFabric(Properties properties, BiFunction<BlockPos, BlockState, ? extends CyclopsBlockEntityCommon> blockEntitySupplier) {
super(properties, blockEntitySupplier);
}

@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}

@Override
protected InteractionResult useWithoutItem(BlockState blockState, Level level, BlockPos pos, Player player, BlockHitResult rayTraceResult) {
InteractionResult activatedSuper = super.useWithoutItem(blockState, level, pos, player, rayTraceResult);
if (activatedSuper.consumesAction()) {
return activatedSuper;
}

if (BlockFlopperConfig.showContentsStatusMessageOnClick) {
Storage<FluidVariant> storage = FluidStorage.SIDED.find(level, pos, rayTraceResult.getDirection());
if (storage != null) {
for (StorageView<FluidVariant> view : storage) {
if (view.isResourceBlank()) {
player.displayClientMessage(Component.literal("0 / "
+ String.format("%,d", view.getCapacity())), true);
} else {
player.displayClientMessage(FluidVariantAttributes.getName(view.getResource()).plainCopy()
.append(Component.literal(": "
+ String.format("%,d", view.getAmount()) + " / "
+ String.format("%,d", view.getCapacity()))), true);
}
}
}
}
return InteractionResult.PASS;
}

@Override
protected ItemInteractionResult useItemOn(ItemStack itemStack, BlockState blockState, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult rayTraceResult) {
ItemInteractionResult activatedSuper = super.useItemOn(itemStack, blockState, level, pos, player, hand, rayTraceResult);
if (activatedSuper.consumesAction()) {
return activatedSuper;
}

IFluidHelpersFabric fh = FlopperFabric._instance.getModHelpers().getFluidHelpers();
Storage<FluidVariant> storageFlopper = FluidStorage.SIDED.find(level, pos, rayTraceResult.getDirection());
ContainerItemContext storageItemContext = ContainerItemContext.forPlayerInteraction(player, hand);
Storage<FluidVariant> storageItem = storageItemContext.find(FluidStorage.ITEM);
if (storageFlopper != null && storageItem != null) {
long movedSimulate;
if (!player.isCrouching()
&& (movedSimulate = fh.moveFluid(storageItem, storageFlopper, fh.getBucketVolume(), player, true)) > 0) {
// Move fluid from the item into the tank if not sneaking
fh.moveFluid(storageItem, storageFlopper, movedSimulate, player, false);
return ItemInteractionResult.SUCCESS;
} else if (player.isCrouching()
&& (movedSimulate = fh.moveFluid(storageFlopper, storageItem, fh.getBucketVolume(), player, true)) > 0) {
// Move fluid from the tank into the item if sneaking
fh.moveFluid(storageFlopper, storageItem, movedSimulate, player, false);
return ItemInteractionResult.SUCCESS;
}
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.cyclops.flopper.block;

/**
* @author rubensworks
*/
public interface IPlayerDisableableSneak {

public void setTemporarilyDisableSneak(boolean setTemporarilyDisableSneak);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.cyclops.flopper.blockentity;

import com.google.common.collect.Sets;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
import net.minecraft.world.level.block.entity.BlockEntityType;
import org.cyclops.cyclopscore.init.ModBaseFabric;
import org.cyclops.flopper.FlopperFabric;
import org.cyclops.flopper.RegistryEntries;
import org.cyclops.flopper.block.BlockFlopperConfig;
import org.cyclops.flopper.client.render.blockentity.RenderBlockEntityFlopperFabric;

/**
* @author rubensworks
*/
public class BlockEntityFlopperConfigFabric extends BlockEntityFlopperConfig<BlockEntityFlopperFabric, ModBaseFabric<?>> {
public BlockEntityFlopperConfigFabric() {
super(
FlopperFabric._instance,
(eConfig) -> new BlockEntityType<>(BlockEntityFlopperFabric::new,
Sets.newHashSet(RegistryEntries.BLOCK_FLOPPER.value()), null)
);
}

@Override
public void onForgeRegistered() {
super.onForgeRegistered();

// Fluid capability
FluidStorage.SIDED.registerForBlockEntity((blockEntity, direction) -> blockEntity.getTank(), getInstance());

// Rendering
if (getMod().getModHelpers().getMinecraftHelpers().isClientSide() && BlockFlopperConfig.renderFluid) {
getMod().getProxy().registerRenderer(getInstance(), RenderBlockEntityFlopperFabric::new);
}
}
}
Loading

0 comments on commit 6b0b9b8

Please sign in to comment.