Skip to content

Commit

Permalink
Improve planters
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jan 12, 2025
1 parent c318da2 commit e363400
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog-next.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Mechanical Planters can now hold 9 stacks of items (previously could only hold 1).
- Workbench now shows items stored in it.
- Splash particles are no longer saved.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
import eu.pb4.polyfactory.ui.GuiTextures;
import eu.pb4.polyfactory.ui.TagLimitedSlot;
import eu.pb4.polyfactory.util.FactoryUtil;
import eu.pb4.polyfactory.util.inventory.MinimalSidedInventory;
import eu.pb4.polyfactory.util.inventory.SingleStackInventory;
import eu.pb4.polymer.virtualentity.api.attachment.BlockBoundAttachment;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.SidedInventory;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
Expand All @@ -37,8 +41,11 @@
import net.minecraft.world.event.GameEvent;
import org.jetbrains.annotations.Nullable;

public class PlanterBlockEntity extends LockableBlockEntity implements SingleStackInventory, SidedInventory, BlockEntityExtraListener, OwnedBlockEntity {
private ItemStack stack = ItemStack.EMPTY;
import java.util.stream.IntStream;

public class PlanterBlockEntity extends LockableBlockEntity implements MinimalSidedInventory, BlockEntityExtraListener, OwnedBlockEntity {
private static final int[] SLOTS = IntStream.range(0, 9).toArray();
private final DefaultedList<ItemStack> items = DefaultedList.ofSize(9, ItemStack.EMPTY);
protected GameProfile owner = null;
protected double process = 0;
private float stress = 0;
Expand All @@ -51,7 +58,7 @@ public PlanterBlockEntity(BlockPos pos, BlockState state) {

@Override
protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
nbt.put("stack", this.stack.toNbtAllowEmpty(lookup));
Inventories.writeNbt(nbt, this.items, lookup);
nbt.putDouble("progress", this.process);
if (this.owner != null) {
nbt.put("owner", LegacyNbtHelper.writeGameProfile(new NbtCompound(), this.owner));
Expand All @@ -62,7 +69,12 @@ protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {

@Override
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
this.stack = ItemStack.fromNbtOrEmpty(lookup, nbt.getCompound("stack"));
if (nbt.contains("stack", NbtElement.COMPOUND_TYPE) && !nbt.contains("Items", NbtElement.LIST_TYPE)) {
this.setStack(0, ItemStack.fromNbtOrEmpty(lookup, nbt.getCompound("stack")));
} else {
Inventories.readNbt(nbt, this.items, lookup);
}

this.process = nbt.getDouble("progress");
if (nbt.contains("owner")) {
this.owner = LegacyNbtHelper.toGameProfile(nbt.getCompound("owner"));
Expand All @@ -88,19 +100,18 @@ public void setOwner(GameProfile profile) {
}

@Override
public ItemStack getStack() {
return this.stack;
public int[] getAvailableSlots(Direction side) {
return SLOTS;
}

@Override
public void setStack(ItemStack stack) {
this.stack = stack;
this.markDirty();
public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) {
return stack.isIn(FactoryItemTags.ALLOWED_IN_PLANTER);
}

@Override
public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) {
return stack.isIn(FactoryItemTags.ALLOWED_IN_PLANTER);
public boolean canExtract(int slot, ItemStack stack, Direction dir) {
return true;
}

@Override
Expand All @@ -111,11 +122,25 @@ protected void createGui(ServerPlayerEntity playerEntity) {
public static <T extends BlockEntity> void ticker(World world, BlockPos pos, BlockState state, T t) {
var self = (PlanterBlockEntity) t;

if (self.stack.isEmpty() || !(self.stack.getItem() instanceof BlockItem blockItem)) {
if (self.isEmpty()) {
self.stress = 0;
self.process = 0;
return;
}
ItemStack stack = ItemStack.EMPTY;
for (var x : self.items) {
if (!x.isEmpty()) {
stack = x;
break;
}
}

if (!(stack.getItem() instanceof BlockItem blockItem)) {
self.stress = 0;
self.process = 0;
return;
}

var placableState = blockItem.getBlock().getDefaultState();

var speed = Math.abs(RotationUser.getRotation((ServerWorld) world, pos).speed()) * MathHelper.RADIANS_PER_DEGREE * 2.5f;
Expand Down Expand Up @@ -159,7 +184,7 @@ public static <T extends BlockEntity> void ticker(World world, BlockPos pos, Blo
self.process = 0;
self.stress = 0;
world.setBlockState(place, placableState);
self.stack.decrement(1);
stack.decrement(1);
world.playSound(null, pos, placableState.getSoundGroup().getPlaceSound(), SoundCategory.BLOCKS, 0.5f, 1.0f);
world.emitGameEvent(GameEvent.BLOCK_PLACE, place, GameEvent.Emitter.of(placableState));
if (self.owner != null && world.getPlayerByUuid(self.owner.getId()) instanceof ServerPlayerEntity serverPlayer) {
Expand Down Expand Up @@ -192,11 +217,18 @@ public void setRadius(int radius) {
this.markDirty();
}

@Override
public DefaultedList<ItemStack> getStacks() {
return this.items;
}

private class Gui extends SimpleGui {
public Gui(ServerPlayerEntity player) {
super(ScreenHandlerType.HOPPER, player, false);
this.setTitle(GuiTextures.CENTER_SLOT_GENERIC.apply(PlanterBlockEntity.this.getCachedState().getBlock().getName()));
this.setSlotRedirect(2, new TagLimitedSlot(PlanterBlockEntity.this, 0, FactoryItemTags.ALLOWED_IN_PLANTER));
super(ScreenHandlerType.GENERIC_3X3, player, false);
this.setTitle(PlanterBlockEntity.this.getCachedState().getBlock().getName());
for (int i = 0; i < 9; i++) {
this.setSlotRedirect(i, new TagLimitedSlot(PlanterBlockEntity.this, i, FactoryItemTags.ALLOWED_IN_PLANTER));
}
this.open();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

public abstract class TallItemMachineBlockEntity extends LockableBlockEntity implements MachineInfoProvider, InventorySimpleContainerProvider, SidedInventory {
protected Text state;

public TallItemMachineBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

@Mixin(Schema1460.class)
public abstract class Schema1460Mixin extends Schema {
@Shadow protected static void registerInventory(Schema schema, Map<String, Supplier<TypeTemplate>> map, String name) {};

public Schema1460Mixin(int versionKey, Schema parent) {
super(versionKey, parent);
}

@Shadow
protected static void method_5273(Schema schema, Map<String, Supplier<TypeTemplate>> map, String name) {
}


@Inject(method = "registerBlockEntities", at = @At("RETURN"))
private void registerPolyFactoryBlockEntities(Schema schema, CallbackInfoReturnable<Map<String, Supplier<TypeTemplate>>> cir) {
Expand All @@ -42,12 +42,14 @@ private void registerPolyFactoryBlockEntities(Schema schema, CallbackInfoReturna
// return DSL.optionalFields("HeldStack", TypeReferences.ITEM_STACK.in(schema));
//});

method_5273(schema, map, mod("steam_engine"));
method_5273(schema, map, mod("grinder"));
method_5273(schema, map, mod("press"));
method_5273(schema, map, mod("mixer"));
method_5273(schema, map, mod("crafter"));
method_5273(schema, map, mod("workbench"));
registerInventory(schema, map, mod("steam_engine"));
registerInventory(schema, map, mod("grinder"));
registerInventory(schema, map, mod("press"));
registerInventory(schema, map, mod("mixer"));
registerInventory(schema, map, mod("crafter"));
registerInventory(schema, map, mod("workbench"));
registerInventory(schema, map, mod("mechanical_drain"));
registerInventory(schema, map, mod("mechanical_spout"));

container(schema, map, "container");
container(schema, map, "creative_container");
Expand All @@ -59,7 +61,6 @@ private void registerPolyFactoryBlockEntities(Schema schema, CallbackInfoReturna

stackOwner(schema, map, "miner", "tool");
stackOwner(schema, map, "placer", "stack");
stackOwner(schema, map, "planter", "stack");

dataCache(schema, map, "nixie_tube_controller");
dataCache(schema, map, "provider_data_cache");
Expand All @@ -72,15 +73,17 @@ private void registerPolyFactoryBlockEntities(Schema schema, CallbackInfoReturna
;
});

schema.register(map, mod("planter"), (name) -> {
return DSL.optionalFields("stack", TypeReferences.ITEM_STACK.in(schema), "Items", DSL.list(TypeReferences.ITEM_STACK.in(schema)));
});

schema.register(map, mod("splitter"), (name) -> {
return DSL.optionalFields("FilterStackLeft", TypeReferences.ITEM_STACK.in(schema),
"FilterStackRight", TypeReferences.ITEM_STACK.in(schema))
;
});
schema.register(map, mod("windmill"), (name) -> {
return DSL.optionalFields("Sails", DSL.list(TypeReferences.ITEM_STACK.in(schema)))
;
return DSL.optionalFields("Sails", DSL.list(TypeReferences.ITEM_STACK.in(schema)));
});
schema.register(map, mod("wireless_redstone"), (name) -> {
return DSL.optionalFields("key1", TypeReferences.ITEM_STACK.in(schema),
Expand All @@ -96,8 +99,10 @@ private void registerPolyFactoryBlockEntities(Schema schema, CallbackInfoReturna
schema.registerSimple(map, mod("hand_crank"));
schema.registerSimple(map, mod("pump"));
schema.registerSimple(map, mod("pipe"));
schema.registerSimple(map, mod("nozzle"));
schema.registerSimple(map, mod("drain"));
schema.registerSimple(map, mod("filtered_pipe"));
schema.registerSimple(map, mod("redstone_valve_pipe"));
schema.registerSimple(map, mod("fluid_tank"));
schema.registerSimple(map, mod("portable_fluid_tank"));
}
Expand Down

0 comments on commit e363400

Please sign in to comment.