Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to copy and paste nodes with card cloner. #316

Open
wants to merge 5 commits into
base: 1.20.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.20.1 2023-07-05T16:00:54.6414975 Languages: en_us
e5b541adc2afe33fee0b250287b1a4e7405c1d94 assets/laserio/lang/en_us.json
// 1.20.1 2024-11-17T00:17:57.6684095 Languages: en_us
b485785204ffc233284fc5315153eefc8ddbcca6 assets/laserio/lang/en_us.json
6 changes: 6 additions & 0 deletions src/generated/resources/assets/laserio/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@
"laserio.tooltip.item.card.sneaky.SOUTH": "South",
"laserio.tooltip.item.card.sneaky.UP": "Up",
"laserio.tooltip.item.card.sneaky.WEST": "West",
"laserio.tooltip.item.cloner.card.header": "Saved Card: ",
"laserio.tooltip.item.cloner.node.dimension": "Dim: ",
"laserio.tooltip.item.cloner.node.header": "Saved Node: ",
"laserio.tooltip.item.cloner.node.position": "Pos: ",
"laserio.tooltip.item.filter.nbt": "Match NBT: ",
"laserio.tooltip.item.filter.nbt.allow": "True",
"laserio.tooltip.item.filter.nbt.deny": "False",
"laserio.tooltip.item.filter.type": "Type: ",
"laserio.tooltip.item.filter.type.allow": "Allow",
"laserio.tooltip.item.filter.type.deny": "Deny",
"laserio.tooltip.item.show_settings": "Hold shift to show settings",
"message.laserio.cloner.cardcount": "Insufficient materials",
"message.laserio.cloner.nodata": "Nothing to paste",
"message.laserio.wrenchrange": "Connection exceeds maximum range of %d",
"screen.laserio.allowlist": "Allow",
"screen.laserio.alpha": "Alpha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ public void validateConnections(BlockPos originalPos) {
@Override
public void load(CompoundTag tag) {
super.load(tag);
if (tag.contains("laserColor")) {
int wrenchA = tag.contains("wrenchAlpha") ? tag.getInt("wrenchAlpha") : 0;
setColor(new Color(tag.getInt("laserColor"), true), wrenchA);
}
// Return and do not alter connections when loading a cloned node.
if(tag.contains("cloned"))
return;

connections.clear();
ListTag connections = tag.getList("connections", Tag.TAG_COMPOUND);
for (int i = 0; i < connections.size(); i++) {
Expand All @@ -282,10 +290,7 @@ public void load(CompoundTag tag) {
BlockPos originalPos = NbtUtils.readBlockPos(tag.getCompound("myWorldPos"));
if (!originalPos.equals(getBlockPos()) && !originalPos.equals(BlockPos.ZERO))
validateConnections(originalPos);
if (tag.contains("laserColor")) {
int wrenchA = tag.contains("wrenchAlpha") ? tag.getInt("wrenchAlpha") : 0;
setColor(new Color(tag.getInt("laserColor"), true), wrenchA);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.direwolf20.laserio.common.blocks.baseblocks.BaseLaserBlock;
import com.direwolf20.laserio.common.containers.LaserNodeContainer;
import com.direwolf20.laserio.common.containers.customhandler.LaserNodeItemHandler;
import com.direwolf20.laserio.common.items.CardCloner;
import com.direwolf20.laserio.common.items.CardHolder;
import com.direwolf20.laserio.common.items.LaserWrench;
import com.direwolf20.laserio.common.items.cards.BaseCard;
Expand Down Expand Up @@ -81,7 +82,7 @@ public LaserNode() {
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult result) {
ItemStack heldItem = player.getMainHandItem();
if (heldItem.getItem() instanceof LaserWrench)
if (heldItem.getItem() instanceof LaserWrench || heldItem.getItem() instanceof CardCloner)
return InteractionResult.PASS;
if (!level.isClientSide) {
BlockEntity be = level.getBlockEntity(pos);
Expand Down
90 changes: 86 additions & 4 deletions src/main/java/com/direwolf20/laserio/common/items/CardCloner.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package com.direwolf20.laserio.common.items;

import com.direwolf20.laserio.client.blockentityrenders.LaserNodeBERender;
import com.direwolf20.laserio.common.blockentities.LaserNodeBE;
import com.direwolf20.laserio.common.blocks.LaserNode;
import com.direwolf20.laserio.common.containers.CardItemContainer;
import com.direwolf20.laserio.common.containers.LaserNodeContainer;
import com.direwolf20.laserio.common.items.cards.BaseCard;
import com.direwolf20.laserio.common.network.PacketHandler;
import com.direwolf20.laserio.common.network.packets.PacketCopyPasteNode;
import com.direwolf20.laserio.util.*;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
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.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.ItemStackHandler;
Expand Down Expand Up @@ -47,7 +62,23 @@ public void appendHoverText(ItemStack stack, @Nullable Level world, List<Compone
.withStyle(ChatFormatting.GRAY));
} else {
String cardType = getItemType(stack);
MutableComponent toWrite = tooltipMaker("laserio.tooltip.item.filter.type", ChatFormatting.GRAY.getColor());
MutableComponent toWrite;
tooltip.add(Component.translatable("laserio.tooltip.item.cloner.node.header"));
if(stack.getOrCreateTag().contains("nodeData")) {
CompoundTag nodeData = stack.getOrCreateTag().getCompound("nodeData");
toWrite = tooltipMaker("laserio.tooltip.item.cloner.node.position", ChatFormatting.GRAY.getColor());
Style style = Style.EMPTY;
style = style.withColor(ChatFormatting.WHITE.getColor());
toWrite.append(Component.literal(nodeData.getCompound("myWorldPos").toString()).setStyle(style));
tooltip.add(toWrite);

toWrite = tooltipMaker("laserio.tooltip.item.cloner.node.dimension", ChatFormatting.GRAY.getColor());
toWrite.append(tooltipMaker(world.dimension().location().toShortLanguageKey(), ChatFormatting.BLUE.getColor()));
tooltip.add(toWrite);
}

toWrite = tooltipMaker("laserio.tooltip.item.filter.type", ChatFormatting.GRAY.getColor());
tooltip.add(Component.translatable("laserio.tooltip.item.cloner.card.header"));
int cardColor = ChatFormatting.WHITE.getColor();
if (cardType.equals("card_item"))
cardColor = ChatFormatting.GREEN.getColor();
Expand All @@ -66,10 +97,10 @@ else if (cardType.equals("card_redstone"))
return;

CompoundTag compoundTag = stack.getOrCreateTag().getCompound("settings");
int mode = !compoundTag.contains("mode") ? 0 : compoundTag.getByte("mode");;
int mode = !compoundTag.contains("mode") ? 0 : compoundTag.getByte("mode");
String currentMode = BaseCard.TransferMode.values()[mode].toString();
toWrite = tooltipMaker("laserio.tooltip.item.card.mode", ChatFormatting.GRAY.getColor());
int modeColor = ChatFormatting.GRAY.getColor();
int modeColor = ChatFormatting.WHITE.getColor();
if (currentMode.equals("EXTRACT"))
modeColor = ChatFormatting.RED.getColor();
else if (currentMode.equals("INSERT"))
Expand All @@ -82,7 +113,7 @@ else if (currentMode.equals("SENSOR"))
tooltip.add(toWrite);

toWrite = tooltipMaker("laserio.tooltip.item.card.channel", ChatFormatting.GRAY.getColor());
int channel = !compoundTag.contains("channel") ? 0 : compoundTag.getByte("channel");;
int channel = !compoundTag.contains("channel") ? 0 : compoundTag.getByte("channel");
toWrite.append(tooltipMaker(String.valueOf(channel), LaserNodeBERender.colors[channel].getRGB()));
tooltip.add(toWrite);

Expand Down Expand Up @@ -116,6 +147,10 @@ public static void saveSettings(ItemStack stack, CompoundTag tag) {
stack.getOrCreateTag().put("settings", tag);
}

public static void saveNodeData(ItemStack stack, CompoundTag tag){
stack.getOrCreateTag().put("nodeData", tag);
}

public static CompoundTag getSettings(ItemStack stack) {
return stack.getOrCreateTag().getCompound("settings");
}
Expand Down Expand Up @@ -160,4 +195,51 @@ public static ItemStack getOverclocker(ItemStack stack) {
overclockStack = itemStackHandler.getStackInSlot(1);
return overclockStack;
}

public static ItemStackHandler[] getNodeData(ItemStack cloneTool){
ItemStackHandler[] faceHandlers = new ItemStackHandler[Direction.values().length];
if (!(cloneTool.getItem() instanceof CardCloner))
return faceHandlers;
CompoundTag tag = cloneTool.getOrCreateTag();
if (!tag.contains("nodeData")) return faceHandlers;
tag = tag.getCompound("nodeData");
for (int i = 0; i < Direction.values().length; i++) {
faceHandlers[i] = new ItemStackHandler(LaserNodeContainer.CARDSLOTS);
if (tag.contains("Inventory" + i)) {
faceHandlers[i].deserializeNBT(tag.getCompound("Inventory" + i));
}
}
return faceHandlers;
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
ItemStack cloneTool = player.getItemInHand(hand);
if (level.isClientSide()) //No client
return InteractionResultHolder.success(cloneTool);

int range = 10; // How far away you can click on blocks from
BlockHitResult lookingAt = VectorHelper.getLookingAt(player, ClipContext.Fluid.NONE, range);

if (lookingAt == null || !((level.getBlockState(VectorHelper.getLookingAt(player, cloneTool, range).getBlockPos()).getBlock() instanceof LaserNode))) {
if (player.isShiftKeyDown()) {
PacketHandler.sendToServer(new PacketCopyPasteNode(lookingAt.getBlockPos(), PacketCopyPasteNode.NodeCloneModes.CLEAR));
return InteractionResultHolder.pass(cloneTool);
}
}

BlockPos targetPos = lookingAt.getBlockPos();
BlockEntity targetBE = level.getBlockEntity(targetPos);
// Check that targeted block is a laser and bind to variable
if(!(targetBE instanceof LaserNodeBE))
return InteractionResultHolder.pass(cloneTool);

if (player.isShiftKeyDown()) {
PacketHandler.sendToServer(new PacketCopyPasteNode(targetBE.getBlockPos(), PacketCopyPasteNode.NodeCloneModes.COPY));
}
else {
PacketHandler.sendToServer(new PacketCopyPasteNode(targetBE.getBlockPos(), PacketCopyPasteNode.NodeCloneModes.PASTE));
}
return InteractionResultHolder.success(cloneTool);
}
}
36 changes: 32 additions & 4 deletions src/main/java/com/direwolf20/laserio/common/items/CardHolder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.direwolf20.laserio.common.items;

import com.direwolf20.laserio.common.containers.CardHolderContainer;
import com.direwolf20.laserio.common.containers.LaserNodeContainer;
import com.direwolf20.laserio.common.items.cards.BaseCard;
import com.direwolf20.laserio.common.items.filters.BaseFilter;
import com.direwolf20.laserio.common.items.upgrades.OverclockerCard;
Expand All @@ -26,9 +27,7 @@
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;

public class CardHolder extends Item {
public CardHolder() {
Expand Down Expand Up @@ -79,6 +78,19 @@ public void inventoryTick(@NotNull ItemStack stack, @NotNull Level world, @NotNu
}
}

public static Map<Item, Integer> getHolderCardCounts(ItemStack cardHolder){
IItemHandler handler = cardHolder.getCapability(ForgeCapabilities.ITEM_HANDLER, null).orElse(new ItemStackHandler(CardHolderContainer.SLOTS));
Map<Item, Integer> cardCounts = new HashMap<Item, Integer>();
for (int slot = 0; slot < handler.getSlots(); slot++) {
ItemStack card = handler.getStackInSlot(slot);
if (card.isEmpty()) continue;
int currentCardCount = cardCounts.getOrDefault(card.getItem(), 0);
cardCounts.put(card.getItem(), currentCardCount + card.getCount());
BaseCard.getCardContents(card).forEach((k, v) -> cardCounts.merge(k, v, Integer::sum));
}
return cardCounts;
}

public static ItemStack addCardToInventory(ItemStack cardHolder, ItemStack card) {
if (card.getItem() instanceof BaseFilter && card.hasTag())
return card;
Expand Down Expand Up @@ -107,7 +119,23 @@ public static ItemStack addCardToInventory(ItemStack cardHolder, ItemStack card)
return card;
}

public static UUID getUUID(ItemStack stack) {
public static ItemStack requestCardFromInventory(ItemStack cardHolder, ItemStack request) {
IItemHandler handler = cardHolder.getCapability(ForgeCapabilities.ITEM_HANDLER, null).orElse(new ItemStackHandler(CardHolderContainer.SLOTS));
int toRetrieve = request.getCount();
for (int i = handler.getSlots()-1; i >= 0; i--) {
ItemStack stackInSlot = handler.getStackInSlot(i);
if (ItemStack.isSameItem(stackInSlot, request)){
int retreiveCount = Math.min(stackInSlot.getCount(), toRetrieve);
toRetrieve -= retreiveCount;
stackInSlot.shrink(retreiveCount);
}
if (toRetrieve == 0) return request;
}
request.shrink(toRetrieve);
return request;
}

public static UUID getUUID(ItemStack stack) {
CompoundTag nbt = stack.getOrCreateTag();
if (!nbt.hasUUID("UUID")) {
UUID newId = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.direwolf20.laserio.util.MiscTools.tooltipMaker;

Expand Down Expand Up @@ -150,6 +152,21 @@ public static CardItemHandler getInventory(ItemStack stack) {
handler.reSize(CardItemContainer.SLOTS);
return handler;
}
public static Map<Item, Integer> getCardContents(ItemStack card){
Map<Item, Integer> cardContents = new HashMap<Item,Integer>();
if(card.getTag() == null || card.isEmpty())
return cardContents;

CardItemHandler cardItemHandler = BaseCard.getInventory(card);
// These slot assumptions might not be correct(an Energy card has overlcocks in slot 0) Need a polymorphic interface for getInventory.
ItemStack filterSlot = cardItemHandler.getStackInSlot(0);
ItemStack overclockSlot = cardItemHandler.getStackInSlot(1);
if (!filterSlot.isEmpty())
cardContents.put(filterSlot.getItem(), filterSlot.getCount());
if (!overclockSlot.isEmpty())
cardContents.put(overclockSlot.getItem(), overclockSlot.getCount());
return cardContents;
}

public static CardItemHandler setInventory(ItemStack stack, CardItemHandler handler) {
for (int i = 0; i < handler.getSlots(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void register() {
HANDLER.registerMessage(id++, PacketChangeColor.class, PacketChangeColor::encode, PacketChangeColor::decode, PacketChangeColor.Handler::handle);
HANDLER.registerMessage(id++, PacketCopyPasteCard.class, PacketCopyPasteCard::encode, PacketCopyPasteCard::decode, PacketCopyPasteCard.Handler::handle);
//HANDLER.registerMessage(id++, PacketExtractUpgrade.class, PacketExtractUpgrade::encode, PacketExtractUpgrade::decode, PacketExtractUpgrade.Handler::handle);
HANDLER.registerMessage(id++, PacketCopyPasteNode.class, PacketCopyPasteNode::encode, PacketCopyPasteNode::decode, PacketCopyPasteNode.Handler::handle);

//Client Side
HANDLER.registerMessage(id++, PacketNodeParticles.class, PacketNodeParticles::encode, PacketNodeParticles::decode, PacketNodeParticles.Handler::handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static PacketCopyPasteCard decode(FriendlyByteBuf buffer) {
return new PacketCopyPasteCard(buffer.readInt(), buffer.readBoolean());
}

public static void playSound(ServerPlayer player, Holder<SoundEvent> soundEventHolder) {
public static void playSound(ServerPlayer player, Holder<SoundEvent> soundEventHolder, float volume) {
// Get player's position
double x = player.getX();
double y = player.getY();
Expand All @@ -51,7 +51,7 @@ public static void playSound(ServerPlayer player, Holder<SoundEvent> soundEventH
soundEventHolder, // The sound event
SoundSource.MASTER, // The sound category
x, y, z, // The sound location
1, // The volume, 1 is normal, higher is louder
volume, // The volume, 1 is normal, higher is louder
1, // The pitch, 1 is normal, higher is higher pitch
1 // A random for some reason? (Some sounds have different variants, like the enchanting table success
);
Expand All @@ -60,7 +60,12 @@ public static void playSound(ServerPlayer player, Holder<SoundEvent> soundEventH
player.connection.send(packet);
}

public static boolean returnItemToholder(LaserNodeContainer container, ItemStack itemStack, boolean simulate) {
public static void playSound(ServerPlayer player, Holder<SoundEvent> soundEventHolder) {
playSound(player, soundEventHolder, 1f);
}


public static boolean returnItemToholder(LaserNodeContainer container, ItemStack itemStack, boolean simulate) {
if (itemStack.isEmpty()) return true;
int neededReturn = itemStack.getCount();
Map<Integer, Integer> returnStackMap = new HashMap<>();
Expand Down
Loading