Added locking features to drawers

This commit is contained in:
Buuz135 2021-12-22 21:35:32 +01:00
parent c18a62b2e8
commit 408056f9af
9 changed files with 198 additions and 22 deletions

View File

@ -9,6 +9,7 @@ import com.buuz135.functionalstorage.client.DrawerRenderer;
import com.buuz135.functionalstorage.data.FunctionalStorageBlockstateProvider;
import com.buuz135.functionalstorage.data.FunctionalStorageLangProvider;
import com.buuz135.functionalstorage.data.FunctionalStorageTagsProvider;
import com.buuz135.functionalstorage.item.ConfigurationToolItem;
import com.buuz135.functionalstorage.item.LinkingToolItem;
import com.buuz135.functionalstorage.item.StorageUpgradeItem;
import com.buuz135.functionalstorage.item.UpgradeItem;
@ -71,6 +72,7 @@ public class FunctionalStorage extends ModuleController {
public static RegistryObject<Item> PULLING_UPGRADE;
public static RegistryObject<Item> PUSHING_UPGRADE;
public static RegistryObject<Item> VOID_UPGRADE;
public static RegistryObject<Item> CONFIGURATION_TOOL;
public static AdvancedTitaniumTab TAB = new AdvancedTitaniumTab("functionalstorage", true);
@ -100,6 +102,7 @@ public class FunctionalStorage extends ModuleController {
PUSHING_UPGRADE = getRegistries().register(Item.class, "pusher_upgrade", () -> new UpgradeItem(new Item.Properties(), UpgradeItem.Type.UTILITY));
VOID_UPGRADE = getRegistries().register(Item.class, "void_upgrade", () -> new UpgradeItem(new Item.Properties(), UpgradeItem.Type.UTILITY));
ARMORY_CABINET = getRegistries().register(Block.class, "armory_cabinet", ArmoryCabinetBlock::new);
CONFIGURATION_TOOL = getRegistries().register(Item.class, "configuration_tool", ConfigurationToolItem::new);
}
public enum DrawerType{

View File

@ -54,6 +54,11 @@ public class CompactingDrawerTile extends ControllableDrawerTile<CompactingDrawe
public boolean isVoid() {
return CompactingDrawerTile.this.isVoid();
}
@Override
public boolean isLocked() {
return CompactingDrawerTile.this.isLocked();
}
};
lazyStorage = LazyOptional.of(() -> this.handler);
//TODO Check for the recipe on load

View File

@ -191,6 +191,20 @@ public abstract class ControllableDrawerTile<T extends ControllableDrawerTile<T>
return false;
}
public void toggleLocking(){
this.locked = !this.locked;
markComponentDirty();
}
public boolean isLocked() {
return locked;
}
public void setLocked(boolean locked) {
this.locked = locked;
markComponentDirty();
}
@Override
public void invalidateCaps() {
super.invalidateCaps();

View File

@ -76,6 +76,17 @@ public class DrawerControllerTile extends ControllableDrawerTile<DrawerControlle
return 1;
}
@Override
public void toggleLocking() {
super.toggleLocking();
for (Long connectedDrawer : this.connectedDrawers.getConnectedDrawers()) {
BlockEntity blockEntity = this.level.getBlockEntity(BlockPos.of(connectedDrawer));
if (blockEntity instanceof ControllableDrawerTile){
((ControllableDrawerTile<?>) blockEntity).setLocked(this.isLocked());
}
}
}
@NotNull
@Override
public DrawerControllerTile getSelf() {

View File

@ -61,6 +61,11 @@ public class DrawerTile extends ControllableDrawerTile<DrawerTile> {
return DrawerTile.this.hasDowngrade();
}
@Override
public boolean isLocked() {
return DrawerTile.this.isLocked();
}
};
lazyStorage = LazyOptional.of(() -> this.handler);

View File

@ -2,6 +2,7 @@ package com.buuz135.functionalstorage.client;
import com.buuz135.functionalstorage.FunctionalStorage;
import com.buuz135.functionalstorage.block.tile.DrawerTile;
import com.buuz135.functionalstorage.inventory.BigInventoryHandler;
import com.buuz135.functionalstorage.util.NumberUtils;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.vertex.PoseStack;
@ -65,62 +66,65 @@ public class DrawerRenderer implements BlockEntityRenderer<DrawerTile> {
}
private void render1Slot(PoseStack matrixStack, MultiBufferSource bufferIn, int combinedLightIn, int combinedOverlayIn, DrawerTile tile){
if (!tile.getStorage().getStackInSlot(0).isEmpty()){
BigInventoryHandler inventoryHandler = (BigInventoryHandler) tile.getStorage();
if (!inventoryHandler.getStoredStacks().get(0).getStack().isEmpty()){
matrixStack.translate(0.5, 0.5, 0.0005f);
ItemStack stack = tile.getStorage().getStackInSlot(0);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.015f);
ItemStack stack = inventoryHandler.getStoredStacks().get(0).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(0).getAmount(), 0.015f);
}
}
private void render2Slot(PoseStack matrixStack, MultiBufferSource bufferIn, int combinedLightIn, int combinedOverlayIn, DrawerTile tile){
if (!tile.getStorage().getStackInSlot(0).isEmpty()){
BigInventoryHandler inventoryHandler = (BigInventoryHandler) tile.getStorage();
if (!inventoryHandler.getStoredStacks().get(1).getStack().isEmpty()){
matrixStack.pushPose();
matrixStack.translate(0.5, 0.27f, 0.0005f);
matrixStack.scale(0.5f, 0.5f, 1);
ItemStack stack = tile.getStorage().getStackInSlot(0);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.02f);
ItemStack stack = inventoryHandler.getStoredStacks().get(1).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(1).getAmount(), 0.02f);
matrixStack.popPose();
}
if (!tile.getStorage().getStackInSlot(1).isEmpty()){
if (!inventoryHandler.getStoredStacks().get(1).getStack().isEmpty()){
matrixStack.pushPose();
matrixStack.translate(0.5, 0.77f, 0.0005f);
matrixStack.scale(0.5f, 0.5f, 1);
ItemStack stack = tile.getStorage().getStackInSlot(1);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.02f);
ItemStack stack = inventoryHandler.getStoredStacks().get(1).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(1).getAmount(), 0.02f);
matrixStack.popPose();
}
}
private void render4Slot(PoseStack matrixStack, MultiBufferSource bufferIn, int combinedLightIn, int combinedOverlayIn, DrawerTile tile){
if (!tile.getStorage().getStackInSlot(0).isEmpty()){ //BOTTOM RIGHT
BigInventoryHandler inventoryHandler = (BigInventoryHandler) tile.getStorage();
if (!inventoryHandler.getStoredStacks().get(0).getStack().isEmpty()){ //BOTTOM RIGHT
matrixStack.pushPose();
matrixStack.translate(0.75, 0.27f, 0.0005f);
matrixStack.scale(0.5f, 0.5f, 1);
ItemStack stack = tile.getStorage().getStackInSlot(0);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.02f);
ItemStack stack = inventoryHandler.getStoredStacks().get(0).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(0).getAmount(), 0.02f);
matrixStack.popPose();
}
if (!tile.getStorage().getStackInSlot(1).isEmpty()){ //BOTTOM LEFT
if (!inventoryHandler.getStoredStacks().get(1).getStack().isEmpty()){ //BOTTOM LEFT
matrixStack.pushPose();
matrixStack.translate(0.25, 0.27f, 0.0005f);
matrixStack.scale(0.5f, 0.5f, 1);
ItemStack stack = tile.getStorage().getStackInSlot(1);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.02f);
ItemStack stack = inventoryHandler.getStoredStacks().get(1).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(1).getAmount(), 0.02f);
matrixStack.popPose();
}
if (!tile.getStorage().getStackInSlot(2).isEmpty()){ //TOP RIGHT
if (!inventoryHandler.getStoredStacks().get(2).getStack().isEmpty()){ //TOP RIGHT
matrixStack.pushPose();
matrixStack.translate(0.75, 0.77f, 0.0005f);
matrixStack.scale(0.5f, 0.5f, 1);
ItemStack stack = tile.getStorage().getStackInSlot(2);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.02f);
ItemStack stack = inventoryHandler.getStoredStacks().get(2).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(2).getAmount(), 0.02f);
matrixStack.popPose();
}
if (!tile.getStorage().getStackInSlot(3).isEmpty()){ //TOP LEFT
if (!inventoryHandler.getStoredStacks().get(3).getStack().isEmpty()){ //TOP LEFT
matrixStack.pushPose();
matrixStack.translate(0.25, 0.77f, 0.0005f);
matrixStack.scale(0.5f, 0.5f, 1);
ItemStack stack = tile.getStorage().getStackInSlot(3);
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, stack.getCount(), 0.02f);
ItemStack stack = inventoryHandler.getStoredStacks().get(3).getStack();
renderStack(matrixStack, bufferIn, combinedLightIn, combinedOverlayIn, stack, inventoryHandler.getStoredStacks().get(3).getAmount(), 0.02f);
matrixStack.popPose();
}
}

View File

@ -71,7 +71,7 @@ public abstract class BigInventoryHandler implements IItemHandler, INBTSerializa
ItemStack out = bigStack.getStack().copy();
int newAmount = bigStack.getAmount();
if (!simulate) {
bigStack.setStack(ItemStack.EMPTY); //TODO Dont change if locked
if (!isLocked()) bigStack.setStack(ItemStack.EMPTY); //TODO Dont change if locked
bigStack.setAmount(0);
onChange();
}
@ -134,6 +134,12 @@ public abstract class BigInventoryHandler implements IItemHandler, INBTSerializa
public abstract boolean hasDowngrade();
public abstract boolean isLocked();
public List<BigStack> getStoredStacks() {
return storedStacks;
}
public class BigStack{
private ItemStack stack;

View File

@ -73,6 +73,7 @@ public abstract class CompactingInventoryHandler implements IItemHandler, INBTSe
}
public void reset(){
if (isLocked()) return;
this.resultList.forEach(result -> {
result.setResult(ItemStack.EMPTY);
result.setNeeded(1);
@ -160,6 +161,8 @@ public abstract class CompactingInventoryHandler implements IItemHandler, INBTSe
public abstract boolean isVoid();
public abstract boolean isLocked();
public List<CompactingUtil.Result> getResultList() {
return resultList;
}

View File

@ -0,0 +1,125 @@
package com.buuz135.functionalstorage.item;
import com.buuz135.functionalstorage.FunctionalStorage;
import com.buuz135.functionalstorage.block.tile.ControllableDrawerTile;
import com.buuz135.functionalstorage.block.tile.DrawerControllerTile;
import com.hrznstudio.titanium.item.BasicItem;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.*;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.List;
import java.util.Locale;
public class ConfigurationToolItem extends BasicItem {
public static final String NBT_MODE = "Mode";
public ConfigurationToolItem() {
super(new Properties().tab(FunctionalStorage.TAB).stacksTo(1));
}
@Override
public void onCraftedBy(ItemStack p_41447_, Level p_41448_, Player p_41449_) {
super.onCraftedBy(p_41447_, p_41448_, p_41449_);
initNbt(p_41447_);
}
private ItemStack initNbt(ItemStack stack){
stack.getOrCreateTag().putString(NBT_MODE, ConfigurationAction.LOCKING.name());
return stack;
}
@Override
public void fillItemCategory(CreativeModeTab group, NonNullList<ItemStack> items) {
if (allowdedIn(group)) {
items.add(initNbt(new ItemStack(this)));
}
}
@Override
public InteractionResult useOn(UseOnContext context) {
BlockPos pos = context.getClickedPos();
ItemStack stack = context.getItemInHand();
Level level = context.getLevel();
BlockEntity blockEntity = level.getBlockEntity(pos);
ConfigurationAction configuractionAction = ConfigurationAction.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
if (blockEntity instanceof ControllableDrawerTile){
if (configuractionAction == ConfigurationAction.LOCKING){
((ControllableDrawerTile<?>) blockEntity).toggleLocking();
}
return InteractionResult.SUCCESS;
}
return super.useOn(context);
}
@Override
public InteractionResultHolder<ItemStack> use(Level p_41432_, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!stack.isEmpty() && stack.hasTag()){
if (player.isShiftKeyDown()){
ConfigurationAction linkingMode = ConfigurationAction.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
if (linkingMode == ConfigurationAction.LOCKING){
stack.getOrCreateTag().putString(NBT_MODE, ConfigurationAction.NUMBERS.name());
player.displayClientMessage(new TextComponent("Swapped mode to " + ConfigurationAction.NUMBERS.name().toLowerCase(Locale.ROOT)).setStyle(Style.EMPTY.withColor(ConfigurationAction.NUMBERS.getColor())), true);
} else {
stack.getOrCreateTag().putString(NBT_MODE, ConfigurationAction.LOCKING.name());
player.displayClientMessage(new TextComponent("Swapped mode to " + ConfigurationAction.LOCKING.name().toLowerCase(Locale.ROOT)).setStyle(Style.EMPTY.withColor(ConfigurationAction.LOCKING.getColor())), true);
}
}
player.playSound(SoundEvents.ITEM_FRAME_REMOVE_ITEM, 0.5f, 1);
return InteractionResultHolder.success(stack);
}
return super.use(p_41432_, player, hand);
}
@Override
public void addTooltipDetails(@Nullable BasicItem.Key key, ItemStack stack, List<Component> tooltip, boolean advanced) {
super.addTooltipDetails(key, stack, tooltip, advanced);
if (stack.hasTag()){
ConfigurationAction linkingMode = ConfigurationAction.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
if (key == null){
tooltip.add(new TranslatableComponent("linkingtool.linkingmode").withStyle(ChatFormatting.YELLOW)
.append(new TranslatableComponent("linkingtool.linkingmode." + linkingMode.name().toLowerCase(Locale.ROOT) ).withStyle(Style.EMPTY.withColor(linkingMode.getColor()))));
}
}
}
@Override
public boolean hasTooltipDetails(@Nullable BasicItem.Key key) {
return key == null;
}
public enum ConfigurationAction{
LOCKING(TextColor.fromRgb(new Color(40, 131, 250).getRGB())),
NUMBERS(TextColor.fromRgb(new Color(250, 145, 40).getRGB()));
private final TextColor color;
ConfigurationAction(TextColor color) {
this.color = color;
}
public TextColor getColor() {
return color;
}
}
}