Skip to content

Commit

Permalink
Add Item Packer
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 14, 2024
1 parent 2dd5e71 commit 74f1b40
Show file tree
Hide file tree
Showing 21 changed files with 519 additions and 23 deletions.
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.7.+'
id 'fabric-loom' version '1.8.+'
id "com.modrinth.minotaur" version "2.+"
id 'maven-publish'
}
Expand Down Expand Up @@ -77,11 +77,11 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modImplementation 'eu.pb4:polymer-core:0.11.0+1.21.4-rc1'
modImplementation 'eu.pb4:polymer-resource-pack:0.11.0+1.21.4-rc1'
modImplementation 'eu.pb4:polymer-resource-pack-extras:0.11.0+1.21.4-rc1'
modImplementation 'eu.pb4:polymer-autohost:0.11.0+1.21.4-rc1'
modImplementation 'eu.pb4:polymer-virtual-entity:0.11.0+1.21.4-rc1'
modImplementation 'eu.pb4:polymer-core:0.11.2+1.21.4'
modImplementation 'eu.pb4:polymer-resource-pack:0.11.2+1.21.4'
modImplementation 'eu.pb4:polymer-resource-pack-extras:0.11.2+1.21.4'
modImplementation 'eu.pb4:polymer-autohost:0.11.2+1.21.4'
modImplementation 'eu.pb4:polymer-virtual-entity:0.11.2+1.21.4'
modImplementation include('eu.pb4:map-canvas-api:0.5.0+1.21.3')
modImplementation include('eu.pb4:common-protection-api:1.0.0')
modImplementation include('eu.pb4:sidebar-api:0.5.1+1.21.1')
Expand Down
9 changes: 8 additions & 1 deletion changelog-next.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
- Added Item Packer:
- New block that allows you to add and remove items from Shulker Boxes, bundles and any other storage api compatible item!
- You place/remove the storage item by adding it in front/back of the block.
- All other sides can add/remove items from item's inventory.
- Made out of
- Increased damage done by Lava Splash to 2 hearts (from 0.5).
- Increased damage done by Experience Splash x2 (compared to previously).
- Recipes now use Fabric's Convention tag for Stripped Logs
- Recipes now use Fabric's Convention tag for Stripped Logs.
- Fix Funnels pulling items form blocks using Fabric Storage API loosing last stack if the storage becomes empty.
- Fix Conveyors sometimes deleting items.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ org.gradle.parallel=true
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.21.4
yarn_mappings=1.21.4.+build.1
yarn_mappings=1.21.4+build.1
loader_version=0.16.9

# Mod Properties
mod_version = 0.6.0
mod_version = 0.6.1
maven_group = eu.pb4
archives_base_name = polyfactory

# Dependencies
fabric_version=0.110.2+1.21.4
fabric_version=0.110.5+1.21.4
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import eu.pb4.polyfactory.block.mechanical.source.HandCrankBlockEntity;
import eu.pb4.polyfactory.block.mechanical.source.SteamEngineBlockEntity;
import eu.pb4.polyfactory.block.mechanical.source.WindmillBlockEntity;
import eu.pb4.polyfactory.block.other.ColorableBlockEntity;
import eu.pb4.polyfactory.block.other.ContainerBlockEntity;
import eu.pb4.polyfactory.block.other.WirelessRedstoneBlockEntity;
import eu.pb4.polyfactory.block.other.WorkbenchBlockEntity;
import eu.pb4.polyfactory.block.other.*;
import eu.pb4.polyfactory.mixin.util.BlockEntityTypeAccessor;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
Expand Down Expand Up @@ -144,6 +141,9 @@ public class FactoryBlockEntities {
public static final BlockEntityType<RedstoneValvePipeBlockEntity> REDSTONE_VALVE_PIPE = register("redstone_valve_pipe",
FabricBlockEntityTypeBuilder.create(RedstoneValvePipeBlockEntity::new, FactoryBlocks.REDSTONE_VALVE_PIPE));

public static final BlockEntityType<ItemPackerBlockEntity> ITEM_PACKER = register("item_packer",
FabricBlockEntityTypeBuilder.create(ItemPackerBlockEntity::new, FactoryBlocks.ITEM_PACKER));

public static void register() {
var x = (BlockEntityTypeAccessor) BlockEntityType.HOPPER;
var set = ImmutableSet.<Block>builder();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/eu/pb4/polyfactory/block/FactoryBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class FactoryBlocks {
public static final ClutchBlock CLUTCH = register("clutch", Block.Settings.copy(Blocks.STRIPPED_OAK_WOOD), settings -> new ClutchBlock(settings.strength(2.5F).nonOpaque()));
public static final WindmillBlock WINDMILL = register("windmill", Block.Settings.copy(Blocks.STRIPPED_OAK_WOOD), settings -> new WindmillBlock(settings.strength(2.5F).nonOpaque()));
public static final ContainerBlock CONTAINER = register("wooden_container", Block.Settings.copy(Blocks.CHEST), settings -> new ContainerBlock(9 * 5, settings.nonOpaque()));
public static final ItemPackerBlock ITEM_PACKER = register("item_packer", Block.Settings.copy(SPLITTER), ItemPackerBlock::new);
public static final CableBlock CABLE = register("cable", Block.Settings.copy(Blocks.GLASS), settings -> new CableBlock(settings.breakInstantly().nonOpaque()));
public static final Map<Block, WallWithCableBlock> WALL_WITH_CABLE = WallWithCableBlock.MAP;
public static final CabledDataProviderBlock ITEM_COUNTER = register("item_counter", Block.Settings.copy(SPLITTER), CabledDataProviderBlock::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,9 @@ private boolean tryMovingConveyor(World world, BlockPos pos, BlockState state, C
private boolean tryInserting(World world, BlockPos pos, Direction dir, TagKey<Block> requiredTag) {
var x = FactoryUtil.tryInsertingMovable(this, world, this.getPos(), pos, dir, this.getCachedState().get(ConveyorBlock.DIRECTION), requiredTag);

if (x == FactoryUtil.MovableResult.SUCCESS_REGULAR) {
if (this.holdStack == null || this.holdStack.get().isEmpty()) {
this.clearContainer();
this.setDelta(0);
return true;
}
return x != FactoryUtil.MovableResult.FAILURE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ public void getItemFrom(WorldPointer self, Direction pushDirection, Direction re
}

try (var t = Transaction.openOuter()) {
var val = view.extract(view.getResource(), conveyor.getMaxStackCount(view.getResource().toStack()), t);

var resource = view.getResource();
var val = view.extract(view.getResource(), conveyor.getMaxStackCount(resource.toStack()), t);
if (val != 0) {
t.commit();

if (conveyor.pushNew(view.getResource().toStack((int) val))) {
if (conveyor.pushNew(resource.toStack((int) val))) {
conveyor.setMovementPosition(pushDirection.getOpposite() == selfFacing ? 0.15 : 0.5);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import eu.pb4.polyfactory.util.FactoryUtil;
import eu.pb4.polyfactory.util.inventory.MinimalSidedInventory;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.fabricmc.fabric.api.item.v1.FabricItemStack;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.block.entity.BlockEntity;
Expand Down Expand Up @@ -69,7 +70,7 @@ public static <T extends BlockEntity> void tick(World world, BlockPos pos, Block
if (!stack.isEmpty()) {
var value = world.getFuelRegistry().getFuelTicks(stack);
if (value > 0) {
var remainder = stack.getRecipeRemainder();
var remainder = ((FabricItemStack) (Object) stack).getRecipeRemainder();
stack.decrement(1);
self.fuelTicks = value * 10;
self.fuelInitial = self.fuelTicks;
Expand Down
189 changes: 189 additions & 0 deletions src/main/java/eu/pb4/polyfactory/block/other/ItemPackerBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package eu.pb4.polyfactory.block.other;

import eu.pb4.factorytools.api.block.BarrierBasedWaterloggable;
import eu.pb4.factorytools.api.block.FactoryBlock;
import eu.pb4.factorytools.api.virtualentity.BlockModel;
import eu.pb4.factorytools.api.virtualentity.ItemDisplayElementUtil;
import eu.pb4.polyfactory.util.FactoryUtil;
import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.api.attachment.BlockBoundAttachment;
import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment;
import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.SidedInventory;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ModelTransformationMode;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.*;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView;
import net.minecraft.world.tick.ScheduledTickView;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4fStack;
import xyz.nucleoid.packettweaker.PacketContext;


public class ItemPackerBlock extends Block implements FactoryBlock, BlockEntityProvider, BarrierBasedWaterloggable, InventoryProvider {
public static EnumProperty<Direction> FACING = Properties.FACING;

public ItemPackerBlock(Settings settings) {
super(settings);
this.setDefaultState(this.getDefaultState().with(WATERLOGGED, false));
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(FACING);
builder.add(WATERLOGGED);
}

@Override
protected BlockState getStateForNeighborUpdate(BlockState state, WorldView world, ScheduledTickView tickView, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, Random random) {
tickWater(state, world, tickView, pos);
return super.getStateForNeighborUpdate(state, world, tickView, pos, direction, neighborPos, neighborState, random);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (world.getBlockEntity(pos) instanceof ItemPackerBlockEntity be && player instanceof ServerPlayerEntity serverPlayer) {
be.openGui(serverPlayer);
return ActionResult.SUCCESS_SERVER;
}

return super.onUse(state, world, pos, player, hit);
}

@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if (!state.isOf(newState.getBlock())) {
if (world.getBlockEntity(pos) instanceof ItemPackerBlockEntity be) {
ItemScatterer.spawn(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, be.getStack());
world.updateComparators(pos, this);
}
}
super.onStateReplaced(state, world, pos, newState, moved);

}

@Override
public boolean hasComparatorOutput(BlockState state) {
return true;
}

@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
if (world.getBlockEntity(pos) instanceof ItemPackerBlockEntity be) {
return (int) ((be.getFilledAmount() * 15) / Math.max(be.getFillCapacity(), 1));
}
return 0;
}

@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return waterLog(ctx, this.getDefaultState().with(FACING, ctx.getPlayerLookDirection().getOpposite()));
}

@Override
public FluidState getFluidState(BlockState state) {
return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
}

@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return FactoryUtil.transform(state, rotation::rotate, FACING);
}

@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return FactoryUtil.transform(state, mirror::apply, FACING);
}

@Override
public BlockState getPolymerBreakEventBlockState(BlockState state, PacketContext context) {
return Blocks.OAK_PLANKS.getDefaultState();
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new ItemPackerBlockEntity(pos, state);
}

@Override
public ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) {
return new Model(world, pos, initialBlockState);
}

@Override
public SidedInventory getInventory(BlockState state, WorldAccess world, BlockPos pos) {
return null;
}

public final class Model extends BlockModel {
private final Matrix4fStack mat = new Matrix4fStack(2);
private final ItemDisplayElement mainElement;
private final ItemDisplayElement itemElement;

private Model(ServerWorld world, BlockPos pos, BlockState state) {
this.mainElement = ItemDisplayElementUtil.createSimple(ItemPackerBlock.this.asItem());

this.itemElement = ItemDisplayElementUtil.createSimple();
this.itemElement.setDisplaySize(1, 1);
this.itemElement.setModelTransformation(ModelTransformationMode.NONE);
this.itemElement.setViewRange(0.4f);

//this.countElement.setShadow(true);

this.updateFacing(state);
this.addElement(this.mainElement);
this.addElement(this.itemElement);
}

public void setDisplay(ItemStack stack) {
this.itemElement.setItem(stack.copy());
this.tick();
}

private void updateFacing(BlockState facing) {
var rot = facing.get(FACING).getRotationQuaternion().mul(Direction.NORTH.getRotationQuaternion());
mat.clear();
mat.rotate(rot);
mat.pushMatrix();
mat.rotateY(MathHelper.PI);
mat.scale(2f);
this.mainElement.setTransformation(mat);
mat.popMatrix();

mat.pushMatrix();
//mat.translate(0, 1f / 16f, -6.2f / 16f);
mat.scale(10 / 16f);
this.itemElement.setTransformation(mat);
mat.popMatrix();
this.tick();
}

@Override
public void notifyUpdate(HolderAttachment.UpdateType updateType) {
if (updateType == BlockBoundAttachment.BLOCK_STATE_UPDATE) {
this.updateFacing(this.blockState());
}
}
}
}
Loading

0 comments on commit 74f1b40

Please sign in to comment.