Skip to content

Commit

Permalink
Initial sketch of sticky packages (#13)
Browse files Browse the repository at this point in the history
Putting a slimeblock or honey block next to a package makes it sticky.
Sticky packages remember items put into them and continue to filter
against then even when the contents drop below 0.

Removing the block will make it not sticky, the sticky stack will be
forgotten. This is not a property that is saved to the package item btw,
it's just on the block.

Manual insertion is currently buggy, gotta update the sticky stack. Also
want to add some more effects too
  • Loading branch information
quat1024 committed Oct 10, 2022
1 parent 821c399 commit a5065b2
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import agency.highlysuspect.packages.container.PMenuTypes;
import agency.highlysuspect.packages.item.PItems;
import agency.highlysuspect.packages.junk.PDispenserBehaviors;
import agency.highlysuspect.packages.junk.PItemTags;
import agency.highlysuspect.packages.junk.PTags;
import agency.highlysuspect.packages.junk.PSoundEvents;
import agency.highlysuspect.packages.junk.SidedProxy;
import agency.highlysuspect.packages.net.PNetCommon;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void earlySetup() {
PItems.onInitialize(plat);

PDispenserBehaviors.onInitialize(plat);
PItemTags.onInitialize();
PTags.onInitialize();

PMenuTypes.onInitialize(plat);
PNetCommon.onInitialize(plat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(world.getBlockEntity(pos));
}

@Override
public void neighborChanged(BlockState state, Level level, BlockPos pos, Block fromBlock, BlockPos fromPos, boolean moved) {
super.neighborChanged(state, level, pos, fromBlock, fromPos, moved);
if(level.getBlockEntity(pos) instanceof PackageBlockEntity pkg) pkg.updateStickyStack();
}

@Override
public void playerWillDestroy(Level world, BlockPos pos, BlockState state, Player player) {
super.playerWillDestroy(world, pos, state, player);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agency.highlysuspect.packages.block;

import agency.highlysuspect.packages.Packages;
import agency.highlysuspect.packages.junk.PTags;
import agency.highlysuspect.packages.junk.PackageContainer;
import agency.highlysuspect.packages.junk.PackageStyle;
import agency.highlysuspect.packages.net.PackageAction;
Expand All @@ -9,6 +10,7 @@
import it.unimi.dsi.fastutil.ints.IntIterator;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
Expand Down Expand Up @@ -47,6 +49,7 @@ public PackageBlockEntity(BlockPos pos, BlockState state) {

private PackageStyle style = PackageStyle.ERROR_LOL;
private final PackageContainer container = new PackageContainer().addListener(c -> this.setChanged());
private ItemStack stickyStack = ItemStack.EMPTY;

private Component customName;

Expand All @@ -62,6 +65,44 @@ public PackageContainer getContainer() {
return container;
}

//Stickiness
public boolean canBeSticky() {
if(level == null) return false;

for(Direction dir : Direction.values()) {
if(level.getBlockState(getBlockPos().relative(dir)).is(PTags.STICKY)) return true;
}
return false;
}

public void updateStickyStack() {
boolean anythingChanged = updateStickyStack0();
if(anythingChanged) setChanged();
}

//Returns whether the sticky stack changed. A little caution is needed because updateStickyStack0 is called from setChanged...
//this is because containers call setChanged when they change the contents, and if the stickystack is empty i need to watch for
//items to be added to the container.
private boolean updateStickyStack0() {
//If the package is not allowed to be sticky, clear the sticky stack
if(!canBeSticky()) {
stickyStack = ItemStack.EMPTY;
return true;
}

//If the package is allowed to be sticky, but doesn't have a filter stack, pick one.
//Empty containers will return ItemStack.EMPTY here
if(stickyStack.isEmpty()) {
stickyStack = container.getFilterStack().copy();
return !stickyStack.isEmpty(); //return whether i picke dup a new sticky stack
}
return false;
}

public ItemStack getStickyStack() {
return stickyStack;
}

//<editor-fold desc="Interactions">
public void performAction(Player player, InteractionHand hand, PackageAction action) {
boolean didAnything;
Expand Down Expand Up @@ -270,13 +311,21 @@ public boolean stillValid(Player player) {

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
if(!stickyStack.isEmpty() && !ItemStack.isSameItemSameTags(stickyStack, stack)) return false;
return container.canPlaceItem(slot, stack);
}

@Override
public void clearContent() {
container.clearContent();
}

@Override
public void setChanged() {
updateStickyStack0();
super.setChanged();
if(level != null && !level.isClientSide) level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}
//</editor-fold>

//<editor-fold desc="Nameable">
Expand Down Expand Up @@ -306,6 +355,7 @@ public void setCustomName(Component customName) {
//</editor-fold>

//<editor-fold desc="RenderAttachmentBlockEntity">
@SuppressWarnings("unused")
@SoftImplement("net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity")
public Object getRenderAttachmentData() {
return getStyle();
Expand All @@ -317,7 +367,11 @@ public Object getRenderAttachmentData() {
public void saveAdditional(CompoundTag tag) {
tag.put(PackageContainer.KEY, container.toTag());
tag.put(PackageStyle.KEY, style.toTag());

if(customName != null) tag.putString("CustomName", Component.Serializer.toJson(customName));

tag.put("StickyStack", stickyStack.save(new CompoundTag()));

super.saveAdditional(tag);
}

Expand All @@ -326,14 +380,11 @@ public void load(CompoundTag tag) {
super.load(tag);
container.readFromTag(tag.getCompound(PackageContainer.KEY));
style = PackageStyle.fromTag(tag.getCompound(PackageStyle.KEY));

if(tag.contains("CustomName", 8)) customName = Component.Serializer.fromJson(tag.getString("CustomName"));
else customName = null;
}

@Override
public void setChanged() {
super.setChanged();
if(level != null && !level.isClientSide) level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);

stickyStack = ItemStack.of(tag.getCompound("StickyStack"));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import agency.highlysuspect.packages.Packages;
import agency.highlysuspect.packages.container.PackageMakerMenu;
import agency.highlysuspect.packages.item.PItems;
import agency.highlysuspect.packages.junk.PItemTags;
import agency.highlysuspect.packages.junk.PTags;
import agency.highlysuspect.packages.junk.PSoundEvents;
import agency.highlysuspect.packages.junk.PackageMakerStyle;
import agency.highlysuspect.packages.platform.SoftImplement;
Expand Down Expand Up @@ -55,7 +55,7 @@ public static boolean matchesFrameSlot(ItemStack stack) {

Item item = stack.getItem();
if(!(item instanceof BlockItem)) return false;
if(stack.is(PItemTags.BANNED_FROM_PACKAGE_MAKER)) return false;
if(stack.is(PTags.BANNED_FROM_PACKAGE_MAKER)) return false;

Block b = ((BlockItem) item).getBlock();
BlockState state = b.defaultBlockState();
Expand All @@ -68,13 +68,13 @@ public static boolean matchesInnerSlot(ItemStack stack) {

public static boolean matchesDyeSlot(ItemStack stack) {
if(stack.isEmpty()) return false;
if(stack.is(PItemTags.BANNED_FROM_PACKAGE_MAKER)) return false;
if(stack.is(PTags.BANNED_FROM_PACKAGE_MAKER)) return false;
return stack.getItem() instanceof DyeItem;
}

public static boolean matchesExtraSlot(ItemStack stack) {
if(stack.isEmpty()) return false;
else return stack.is(PItemTags.THINGS_YOU_NEED_FOR_PACKAGE_CRAFTING);
else return stack.is(PTags.THINGS_YOU_NEED_FOR_PACKAGE_CRAFTING);
}

//Static because it's called from PackageMakerScreen, which doesn't have a blockentity available, to show the preview slot
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package agency.highlysuspect.packages.client;

import agency.highlysuspect.packages.Packages;
import agency.highlysuspect.packages.block.PackageBlock;
import agency.highlysuspect.packages.block.PackageBlockEntity;
import agency.highlysuspect.packages.client.PackageActionBinding.MainTrigger;
Expand Down Expand Up @@ -49,7 +48,6 @@ public static boolean performPunchAction(Player player, Level level, BlockPos po
} else return false;
}

//TODO This is probably really janky, yo ucan tell it was mechanically ported from fabric api callbacks
public static void onInitializeClient(ClientPlatformSupport plat) {
plat.installEarlyClientsideLeftClickCallback((player, level, pos, direction) -> {
if(!canAttack(player, level, pos, direction)) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public void render(PackageBlockEntity blockEntity, float tickDelta, PoseStack ma
if(!(packageState.getBlock() instanceof PackageBlock)) return;
TwelveDirection packageTwelveDir = packageState.getValue(PackageBlock.FACING);
PackageContainer container = blockEntity.getContainer();

ItemStack stack = container.getFilterStack();
if(stack.isEmpty()) stack = blockEntity.getStickyStack();

//The block is solid, so has no light inside; use the light of whatever's in front instead.
light = LevelRenderer.getLightColor(world, blockEntity.getBlockPos().relative(packageTwelveDir.primaryDirection));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import net.minecraft.core.Registry;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

public class PItemTags {
public class PTags {
public static TagKey<Item> BANNED_FROM_PACKAGE_MAKER;
public static TagKey<Item> BANNED_FROM_PACKAGE;
public static TagKey<Item> THINGS_YOU_NEED_FOR_PACKAGE_CRAFTING;

public static TagKey<Block> STICKY;

public static void onInitialize() {
BANNED_FROM_PACKAGE_MAKER = TagKey.create(Registry.ITEM_REGISTRY, Packages.id("banned_from_package_maker"));
BANNED_FROM_PACKAGE = TagKey.create(Registry.ITEM_REGISTRY, Packages.id("banned_from_package"));
THINGS_YOU_NEED_FOR_PACKAGE_CRAFTING = TagKey.create(Registry.ITEM_REGISTRY, Packages.id("things_you_need_for_package_crafting"));

STICKY = TagKey.create(Registry.BLOCK_REGISTRY, Packages.id("sticky"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public float fillPercentage() {
//Whether you're ever allowed to put this item into a Package.
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean allowedInPackageAtAll(ItemStack stack) {
if(stack.is(PItemTags.BANNED_FROM_PACKAGE)) return false;
if(stack.is(PTags.BANNED_FROM_PACKAGE)) return false;

boolean checkCanFitInsideContainerItems = true;
if(stack.getItem() instanceof BlockItem bi && bi.getBlock() instanceof ShulkerBoxBlock) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"replace": "false",
"values": [
"minecraft:slime_block",
"minecraft:honey_block"
]
}

0 comments on commit a5065b2

Please sign in to comment.