Fixed issues for items without nbt, closes #2 closes #3

This commit is contained in:
Buuz135 2021-12-29 23:10:40 +01:00
parent bd811b861c
commit 1441cf69fe
6 changed files with 116 additions and 100 deletions

View File

@ -12,7 +12,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle'
group = 'com.buuz135'
version = '1.18.1-0.0.2'
version = '1.18.1-0.0.4'
java {
archivesBaseName = 'functionalstorage'

View File

@ -9,8 +9,8 @@ import com.buuz135.functionalstorage.client.ControllerRenderer;
import com.buuz135.functionalstorage.client.DrawerRenderer;
import com.buuz135.functionalstorage.data.FunctionalStorageBlockTagsProvider;
import com.buuz135.functionalstorage.data.FunctionalStorageBlockstateProvider;
import com.buuz135.functionalstorage.data.FunctionalStorageLangProvider;
import com.buuz135.functionalstorage.data.FunctionalStorageItemTagsProvider;
import com.buuz135.functionalstorage.data.FunctionalStorageLangProvider;
import com.buuz135.functionalstorage.item.ConfigurationToolItem;
import com.buuz135.functionalstorage.item.LinkingToolItem;
import com.buuz135.functionalstorage.item.StorageUpgradeItem;
@ -57,8 +57,8 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@ -158,10 +158,9 @@ public class FunctionalStorage extends ModuleController {
}).subscribe();
EventManager.mod(ColorHandlerEvent.Item.class).process(item -> {
item.getItemColors().register((stack, tint) -> {
if(stack.hasTag()){
CompoundTag tag = stack.getOrCreateTag();
LinkingToolItem.LinkingMode linkingMode = LinkingToolItem.LinkingMode.valueOf(tag.getString(LinkingToolItem.NBT_MODE));
LinkingToolItem.ActionMode linkingAction = LinkingToolItem.ActionMode.valueOf(tag.getString(LinkingToolItem.NBT_ACTION));
LinkingToolItem.LinkingMode linkingMode = LinkingToolItem.getLinkingMode(stack);
LinkingToolItem.ActionMode linkingAction = LinkingToolItem.getActionMode(stack);
if (tint == 3 && tag.contains(LinkingToolItem.NBT_CONTROLLER)) {
return Color.RED.getRGB();
}
@ -171,17 +170,13 @@ public class FunctionalStorage extends ModuleController {
if (tint == 2) {
return linkingAction.getColor().getValue();
}
}
return 0xffffff;
}, LINKING_TOOL.get());
item.getItemColors().register((stack, tint) -> {
if (stack.hasTag()){
CompoundTag tag = stack.getOrCreateTag();
ConfigurationToolItem.ConfigurationAction action = ConfigurationToolItem.ConfigurationAction.valueOf(tag.getString(ConfigurationToolItem.NBT_MODE));
ConfigurationToolItem.ConfigurationAction action = ConfigurationToolItem.getAction(stack);
if (tint == 1) {
return action.getColor().getValue();
}
}
return 0xffffff;
}, CONFIGURATION_TOOL.get());
}).subscribe();

View File

@ -132,7 +132,7 @@ public abstract class ControllableDrawerTile<T extends ControllableDrawerTile<T>
if (!stack.isEmpty()){
Item item = stack.getItem();
if (item.equals(FunctionalStorage.PULLING_UPGRADE.get())){
Direction direction = Direction.byName(stack.getOrCreateTag().getString("Direction"));
Direction direction = UpgradeItem.getDirection(stack);
TileUtil.getTileEntity(level, pos.relative(direction)).ifPresent(blockEntity1 -> {
blockEntity1.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).ifPresent(iItemHandler -> {
for (int otherSlot = 0; otherSlot < iItemHandler.getSlots(); otherSlot++) {
@ -153,7 +153,7 @@ public abstract class ControllableDrawerTile<T extends ControllableDrawerTile<T>
});
}
if (item.equals(FunctionalStorage.PUSHING_UPGRADE.get())){
Direction direction = Direction.byName(stack.getOrCreateTag().getString("Direction"));
Direction direction = UpgradeItem.getDirection(stack);
TileUtil.getTileEntity(level, pos.relative(direction)).ifPresent(blockEntity1 -> {
blockEntity1.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).ifPresent(otherHandler -> {
for (int otherSlot = 0; otherSlot < getStorage().getSlots(); otherSlot++) {
@ -174,7 +174,7 @@ public abstract class ControllableDrawerTile<T extends ControllableDrawerTile<T>
});
}
if (item.equals(FunctionalStorage.COLLECTOR_UPGRADE.get())){
Direction direction = Direction.byName(stack.getOrCreateTag().getString("Direction"));
Direction direction = UpgradeItem.getDirection(stack);
AABB box = new AABB(pos.relative(direction));
for (ItemEntity entitiesOfClass : level.getEntitiesOfClass(ItemEntity.class, box)) {
ItemStack pulledStack = ItemHandlerHelper.copyStackWithSize(entitiesOfClass.getItem(), Math.min(entitiesOfClass.getItem().getCount(), 4));

View File

@ -2,12 +2,10 @@ 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.*;
@ -32,6 +30,13 @@ public class ConfigurationToolItem extends BasicItem {
public static final String NBT_MODE = "Mode";
public static ConfigurationAction getAction(ItemStack stack) {
if (stack.hasTag()) {
return ConfigurationAction.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
}
return ConfigurationAction.LOCKING;
}
public ConfigurationToolItem() {
super(new Properties().tab(FunctionalStorage.TAB).stacksTo(1));
}
@ -60,7 +65,7 @@ public class ConfigurationToolItem extends BasicItem {
ItemStack stack = context.getItemInHand();
Level level = context.getLevel();
BlockEntity blockEntity = level.getBlockEntity(pos);
ConfigurationAction configuractionAction = ConfigurationAction.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
ConfigurationAction configuractionAction = getAction(stack);
if (blockEntity instanceof ControllableDrawerTile) {
if (configuractionAction == ConfigurationAction.LOCKING) {
((ControllableDrawerTile<?>) blockEntity).toggleLocking();
@ -75,9 +80,9 @@ public class ConfigurationToolItem extends BasicItem {
@Override
public InteractionResultHolder<ItemStack> use(Level p_41432_, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!stack.isEmpty() && stack.hasTag()){
if (!stack.isEmpty()) {
if (player.isShiftKeyDown()) {
ConfigurationAction action = ConfigurationAction.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
ConfigurationAction action = getAction(stack);
ConfigurationAction newAction = ConfigurationAction.values()[(Arrays.asList(ConfigurationAction.values()).indexOf(action) + 1) % ConfigurationAction.values().length];
stack.getOrCreateTag().putString(NBT_MODE, newAction.name());
player.displayClientMessage(new TextComponent("Swapped mode to ").setStyle(Style.EMPTY.withColor(newAction.getColor()))
@ -92,8 +97,7 @@ public class ConfigurationToolItem extends BasicItem {
@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));
ConfigurationAction linkingMode = getAction(stack);
if (key == null) {
tooltip.add(new TranslatableComponent("configurationtool.configmode").withStyle(ChatFormatting.YELLOW)
.append(new TranslatableComponent("configurationtool.configmode." + linkingMode.name().toLowerCase(Locale.ROOT)).withStyle(Style.EMPTY.withColor(linkingMode.getColor()))));
@ -101,7 +105,6 @@ public class ConfigurationToolItem extends BasicItem {
tooltip.add(new TranslatableComponent("configurationtool.use").withStyle(ChatFormatting.GRAY));
}
}
}
@Override
public boolean hasTooltipDetails(@Nullable BasicItem.Key key) {

View File

@ -9,11 +9,8 @@ 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.Style;
import net.minecraft.network.chat.TextColor;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.network.chat.*;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
@ -39,6 +36,20 @@ public class LinkingToolItem extends BasicItem {
public static final String NBT_ACTION = "Action";
public static final String NBT_FIRST = "First";
public static LinkingMode getLinkingMode(ItemStack stack) {
if (stack.hasTag() && stack.getTag().contains(NBT_MODE)) {
return LinkingMode.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
}
return LinkingMode.SINGLE;
}
public static ActionMode getActionMode(ItemStack stack) {
if (stack.hasTag() && stack.getTag().contains(NBT_ACTION)) {
return ActionMode.valueOf(stack.getOrCreateTag().getString(NBT_ACTION));
}
return ActionMode.ADD;
}
public LinkingToolItem() {
super(new Properties().tab(FunctionalStorage.TAB).stacksTo(1));
}
@ -68,8 +79,8 @@ public class LinkingToolItem extends BasicItem {
ItemStack stack = context.getItemInHand();
Level level = context.getLevel();
BlockEntity blockEntity = level.getBlockEntity(pos);
LinkingMode linkingMode = LinkingMode.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
ActionMode linkingAction = ActionMode.valueOf(stack.getOrCreateTag().getString(NBT_ACTION));
LinkingMode linkingMode = getLinkingMode(stack);
ActionMode linkingAction = getActionMode(stack);
if (blockEntity instanceof DrawerControllerTile) {
CompoundTag controller = new CompoundTag();
controller.putInt("X", pos.getX());
@ -112,9 +123,9 @@ public class LinkingToolItem extends BasicItem {
@Override
public InteractionResultHolder<ItemStack> use(Level p_41432_, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!stack.isEmpty() && stack.hasTag()){
if (!stack.isEmpty()) {
if (player.isShiftKeyDown()) {
LinkingMode linkingMode = LinkingMode.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
LinkingMode linkingMode = getLinkingMode(stack);
if (linkingMode == LinkingMode.SINGLE) {
stack.getOrCreateTag().putString(NBT_MODE, LinkingMode.MULTIPLE.name());
player.displayClientMessage(new TextComponent("Swapped mode to " + LinkingMode.MULTIPLE.name().toLowerCase(Locale.ROOT)).setStyle(Style.EMPTY.withColor(LinkingMode.MULTIPLE.getColor())), true);
@ -123,7 +134,7 @@ public class LinkingToolItem extends BasicItem {
player.displayClientMessage(new TextComponent("Swapped mode to " + LinkingMode.SINGLE.name().toLowerCase(Locale.ROOT)).setStyle(Style.EMPTY.withColor(LinkingMode.SINGLE.getColor())), true);
}
} else {
ActionMode linkingMode = ActionMode.valueOf(stack.getOrCreateTag().getString(NBT_ACTION));
ActionMode linkingMode = getActionMode(stack);
if (linkingMode == ActionMode.ADD) {
stack.getOrCreateTag().putString(NBT_ACTION, ActionMode.REMOVE.name());
player.displayClientMessage(new TextComponent("Swapped action to " + ActionMode.REMOVE.name().toLowerCase(Locale.ROOT)).setStyle(Style.EMPTY.withColor(ActionMode.REMOVE.getColor())), true);
@ -141,9 +152,8 @@ public class LinkingToolItem extends BasicItem {
@Override
public void addTooltipDetails(@Nullable BasicItem.Key key, ItemStack stack, List<Component> tooltip, boolean advanced) {
super.addTooltipDetails(key, stack, tooltip, advanced);
if (stack.hasTag()){
LinkingMode linkingMode = LinkingMode.valueOf(stack.getOrCreateTag().getString(NBT_MODE));
ActionMode linkingAction = ActionMode.valueOf(stack.getOrCreateTag().getString(NBT_ACTION));
LinkingMode linkingMode = getLinkingMode(stack);
ActionMode linkingAction = getActionMode(stack);
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()))));
@ -158,8 +168,6 @@ public class LinkingToolItem extends BasicItem {
tooltip.add(new TextComponent(""));
tooltip.add(new TranslatableComponent("linkingtool.linkingmode." + linkingMode.name().toLowerCase(Locale.ROOT) + ".desc").withStyle(ChatFormatting.GRAY));
tooltip.add(new TranslatableComponent("linkingtool.use").withStyle(ChatFormatting.GRAY));
}
}
}

View File

@ -26,6 +26,17 @@ import java.util.Locale;
public class UpgradeItem extends BasicItem {
public static Direction getDirection(ItemStack stack){
if (stack.hasTag()){
Item item = stack.getItem();
if (item.equals(FunctionalStorage.PULLING_UPGRADE.get()) || item.equals(FunctionalStorage.PUSHING_UPGRADE.get()) || item.equals(FunctionalStorage.COLLECTOR_UPGRADE.get())){
return Direction.byName(stack.getOrCreateTag().getString("Direction"));
}
}
return Direction.NORTH;
}
private final Type type;
public UpgradeItem(Properties properties, Type type) {
@ -61,10 +72,10 @@ public class UpgradeItem extends BasicItem {
@Override
public boolean overrideOtherStackedOnMe(ItemStack first, ItemStack second, Slot p_150894_, ClickAction clickAction, Player p_150896_, SlotAccess p_150897_) {
if (clickAction == ClickAction.SECONDARY && first.getCount() == 1 && first.hasTag()){
if (clickAction == ClickAction.SECONDARY && first.getCount() == 1){
Item item = first.getItem();
if (item.equals(FunctionalStorage.PULLING_UPGRADE.get()) || item.equals(FunctionalStorage.PUSHING_UPGRADE.get()) || item.equals(FunctionalStorage.COLLECTOR_UPGRADE.get())){
Direction direction = Direction.byName(first.getOrCreateTag().getString("Direction"));
Direction direction = getDirection(first);
Direction next = Direction.values()[(Arrays.asList(Direction.values()).indexOf(direction) + 1 ) % Direction.values().length];
first.getOrCreateTag().putString("Direction", next.name());
p_150896_.playSound(SoundEvents.UI_BUTTON_CLICK, 0.5f, 1);
@ -78,10 +89,9 @@ public class UpgradeItem extends BasicItem {
public void addTooltipDetails(@Nullable BasicItem.Key key, ItemStack stack, List<Component> tooltip, boolean advanced) {
super.addTooltipDetails(key, stack, tooltip, advanced);
tooltip.add(new TranslatableComponent("upgrade.type").withStyle(ChatFormatting.YELLOW).append(new TranslatableComponent("upgrade.type." + getType().name().toLowerCase(Locale.ROOT)).withStyle(ChatFormatting.WHITE)));
if (!stack.hasTag()) return;
Item item = stack.getItem();
if (item.equals(FunctionalStorage.PULLING_UPGRADE.get()) || item.equals(FunctionalStorage.PUSHING_UPGRADE.get()) || item.equals(FunctionalStorage.COLLECTOR_UPGRADE.get())){
tooltip.add(new TranslatableComponent("item.utility.direction").withStyle(ChatFormatting.YELLOW).append(new TranslatableComponent(WordUtils.capitalize(stack.getTag().getString("Direction").toLowerCase(Locale.ROOT))).withStyle(ChatFormatting.WHITE)));
tooltip.add(new TranslatableComponent("item.utility.direction").withStyle(ChatFormatting.YELLOW).append(new TranslatableComponent(WordUtils.capitalize(getDirection(stack).name().toLowerCase(Locale.ROOT))).withStyle(ChatFormatting.WHITE)));
tooltip.add(new TextComponent(""));
tooltip.add(new TranslatableComponent("item.utility.direction.desc").withStyle(ChatFormatting.GRAY));
}