Skip to content

Commit

Permalink
Add visible items to workbench (also make it rotate)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jan 10, 2025
1 parent 31debc9 commit 99cd072
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 69 deletions.
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ 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.2+1.21.4'
modImplementation 'eu.pb4:polymer-blocks: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 'eu.pb4:polymer-core:0.11.3+1.21.4'
modImplementation 'eu.pb4:polymer-blocks:0.11.3+1.21.4'
modImplementation 'eu.pb4:polymer-resource-pack:0.11.3+1.21.4'
modImplementation 'eu.pb4:polymer-resource-pack-extras:0.11.3+1.21.4'
modImplementation 'eu.pb4:polymer-autohost:0.11.3+1.21.4'
modImplementation 'eu.pb4:polymer-virtual-entity:0.11.3+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')
modImplementation include('eu.pb4:sgui:1.8.1+1.21.4')
modImplementation include('eu.pb4:sgui:1.8.2+1.21.4')
modImplementation include('eu.pb4:factorytools:0.5.0+1.21.4')
modImplementation include('com.kneelawk.graphlib:graphlib-core-fabric:2.0.3+1.21')

Expand Down
53 changes: 1 addition & 52 deletions changelog-next.txt
Original file line number Diff line number Diff line change
@@ -1,52 +1 @@
- 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.
- Crafted with Wooden Plates, Steel Plates and a Hopper.
- Added Text Input:
- New data block that allows you to send text data in easy and flexible way.
- When clicked, it will open ui allowing you to input text and then accept sending it.
- Supports multiple input modes: string, integer, decimal and boolean
- Has 1 output.
- Crafted with Steel Plates, Cable, Iron Ingot, Treated Dried Kelp, Redstone Chip and Lightweight (Golden) Pressure Plate.
- Added Data Comparator:
- New data block, that allows you to compare 2 data inputs, getting single output (same as Arithmetic Operator).
- Returns Boolean True or False data, depending on operation set with a wrench.
- Can check if data is `equal`, `not equal`, `less than`, `less or equal`, `more than` and `more or equal`.
- By default it tries to convert types if they don't match, but you can enable strict mode preventing that.
- This block also emits weak redstone signal in direction of output with power level 15 when returning true and 0 for false.
- Crafted with Steel Plate, Cable, Nether Quartz and Redstone Chip.
- Added Fixture Lamp:
- New variant of Colored Lamp block.
- Has small wall-adjusted hitbox.
- Crafted with a Colored Lamp and any Wooden Plate.
- Added Gated Cable:
- When powered by redstone, it prevents any data to pass through it.
- It doesn't add any delay when sending data.
- Can be inverted with a wrench, only allowing data to pass when powered.
- Crafted with Steel Plate, Cable, Smooth Stone Slab, Redstone and Copper Ingot
- Slime and Honey can now be shot out of Fluid Nozzle / Fluid Pressure Gun.
- Added Slime and Honey splashes.
- They deal no damage, instead giving player Sticky effect.
- Sticky effect makes player movement speed slower by 20%, attack speed by 15% and block breaking by 15%.
- When entity with this effect touches a wall while falling, it will slowly slide on it (similar effect to Honey Block).
- Steel Gears can now be placed like regular blocks, if player has an Axle in inventory (unless creative, where it isn't needed). You can also still place it directly on Axle.
- Nixie Tube Controller, Holographic Display and Arithmetic Operator can now disable channels, making them ignore all inputs.
- Fixed Steel Button top texture not looking correctly.
- Increased damage done by Lava Splash to 2 hearts (from 0.5).
- Increased damage done by Experience Splash x2 (compared to previously).
- Increased change of Crushed Raw Ore doubling from 33% to 40%
- Steel Gears use 3d models when held in hand (2d texture otherwise).
- Changed recipe of Arithmetic Operator and Data Memory to include Cable.
- Boolean Data now returns 15 redstone power for true and 0 for false.
- Increased view range of Colored and Caged Lamps.
- Added Grinder recipe for Wind Charges and Eyeblossom dyes.
- Added Press recipe for Bundle and Mace.
- Added Mixer recipe for dyed Bundle and Shulker Box.
- Added advancements related to new content.
- Changed order of colored items in creative item groups (Variants tab) to match other vanilla items.
- Recipes now use Fabric's Convention tag for Stripped Logs.
- Fixed Funnels pulling items form blocks using Fabric Storage API loosing last stack if the storage becomes empty.
- Fixed Conveyors sometimes deleting items.
- Fixed Splashes shoot through Fluid Nozzle not interacting with owner straight away.
- Fixed Boolean Data returning inverted numbers.
- Workbench now shows items stored in it.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.parallel=true
loader_version=0.16.9

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

Expand Down
56 changes: 48 additions & 8 deletions src/main/java/eu/pb4/polyfactory/block/other/WorkbenchBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import eu.pb4.factorytools.api.virtualentity.BlockModel;
import eu.pb4.factorytools.api.virtualentity.ItemDisplayElementUtil;
import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.api.attachment.BlockAwareAttachment;
import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment;
import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
Expand All @@ -19,24 +21,30 @@
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ModelTransformationMode;
import net.minecraft.screen.ScreenHandler;
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.ActionResult;
import net.minecraft.util.ItemScatterer;
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.WorldView;
import net.minecraft.world.tick.ScheduledTickView;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import xyz.nucleoid.packettweaker.PacketContext;

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

public WorkbenchBlock(Settings settings) {
super(settings);
Expand All @@ -45,13 +53,14 @@ public WorkbenchBlock(Settings settings) {
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(WATERLOGGED);
builder.add(FACING);
super.appendProperties(builder);
}

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

@Override
Expand Down Expand Up @@ -80,11 +89,6 @@ public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return world.getBlockEntity(pos) instanceof WorkbenchBlockEntity be ? ScreenHandler.calculateComparatorOutput((Inventory) be) : 0;
}

@Override
public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
super.onPlaced(world, pos, state, placer, itemStack);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (!player.isSneaking() && world.getBlockEntity(pos) instanceof WorkbenchBlockEntity be) {
Expand All @@ -101,8 +105,6 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
super.onStateReplaced(state, world, pos, newState, moved);
}



@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
Expand All @@ -116,11 +118,49 @@ public ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockS

public static class Model extends BlockModel {
private final ItemDisplayElement base;
private final ItemDisplayElement[] items = new ItemDisplayElement[9];

private Model(BlockState state) {
this.base = ItemDisplayElementUtil.createSimple(state.getBlock().asItem());
this.base.setScale(new Vector3f(2));
for (int i = 0; i < 9; i++) {
var element = ItemDisplayElementUtil.createSimple();
element.setViewRange(0.4f);
element.setScale(new Vector3f(4 / 16f));
element.setLeftRotation(new Quaternionf().rotateX(-MathHelper.HALF_PI));
element.setTranslation(new Vector3f((i % 3 - 1) * 3 / 16f, (8 + 2 / 16f) / 16f , (i / 3 - 1) * 3 / 16f));
this.items[i] = element;
}

this.updateState(state);
for (var el : this.items) {
this.addElement(el);
}
this.addElement(this.base);
}


public void setStack(int i, ItemStack stack) {
this.items[i].setItem(stack.copy());
if (this.getTick() != 0) {
this.items[i].tick();
}
}
@Override
public void notifyUpdate(HolderAttachment.UpdateType updateType) {
super.notifyUpdate(updateType);
if (updateType == BlockAwareAttachment.BLOCK_STATE_UPDATE) {
this.updateState(this.blockState());
this.tick();
}
}

private void updateState(BlockState blockState) {
var yaw = blockState.get(FACING).getPositiveHorizontalDegrees();
for (var el : this.items) {
el.setYaw(yaw);
}
this.base.setYaw(yaw);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.pb4.polyfactory.ui.WorkbenchScreenHandler;
import eu.pb4.polyfactory.util.inventory.CustomInsertInventory;
import eu.pb4.polyfactory.util.inventory.MinimalSidedInventory;
import eu.pb4.polymer.virtualentity.api.attachment.BlockAwareAttachment;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
Expand Down Expand Up @@ -39,6 +40,8 @@ public class WorkbenchBlockEntity extends LockableBlockEntity implements Minimal
@Nullable
private RecipeEntry<CraftingRecipe> currentRecipe;

private WorkbenchBlock.Model model;

public WorkbenchBlockEntity(BlockPos pos, BlockState state) {
super(FactoryBlockEntities.WORKBENCH, pos, state);
}
Expand All @@ -56,6 +59,11 @@ public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
updateResult();
}
super.readNbt(nbt, lookup);
if (this.model != null) {
for (int i = 0; i < 9; i++) {
this.markStackDirty(i);
}
}
}

@Override
Expand Down Expand Up @@ -164,6 +172,27 @@ protected void updateResult() {
result.setStack(0, itemStack);
}

public void markStackDirty(int index) {
if (this.model != null) {
this.model.setStack(index, this.getStack(index));
}
}

@Override
public void setStack(int slot, ItemStack stack) {
MinimalSidedInventory.super.setStack(slot, stack);
this.markStackDirty(slot);
}

@Override
public ItemStack removeStack(int slot, int amount) {
var out = MinimalSidedInventory.super.removeStack(slot, amount);
if (!out.isEmpty()) {
this.markStackDirty(slot);
}
return out;
}

@Override
public void markDirty() {
super.markDirty();
Expand Down Expand Up @@ -197,6 +226,10 @@ public int insertStack(ItemStack itemStack, Direction direction) {
@Override
public void onListenerUpdate(WorldChunk chunk) {
updateResult();
this.model = BlockAwareAttachment.get(chunk, this.pos).holder() instanceof WorkbenchBlock.Model m ? m : null;
for (int i = 0; i < 9; i++) {
this.markStackDirty(i);
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ protected void onCrafted(ItemStack stack) {

for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
this.addSlot(new Slot(this.recipeInput, j + i * 3, 30 + j * 18, 17 + i * 18));
var index = j + i * 3;
this.addSlot(new Slot(this.recipeInput, index, 30 + j * 18, 17 + i * 18) {
@Override
public void markDirty() {
super.markDirty();
input.markStackDirty(index);
}
});
}
}

Expand Down

0 comments on commit 99cd072

Please sign in to comment.