Skip to content

Commit

Permalink
Migrate cable item data to stack components
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-1000 committed Jul 29, 2024
1 parent 10a55d5 commit 52c5b3e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
2 changes: 2 additions & 0 deletions projects/pswg/src/main/java/com/parzivail/pswg/Galaxies.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public void onInitialize()

Resources.checkVersion();

SwgComponents.register();

SwgRecipeType.register();
SwgRecipeSerializers.register();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.parzivail.pswg.container;

import com.parzivail.pswg.Resources;
import net.minecraft.component.ComponentType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.math.BlockPos;

public class SwgComponents
{
public static final ComponentType<BlockPos> CableSource = ComponentType.<BlockPos>builder()
.codec(BlockPos.CODEC)
.packetCodec(BlockPos.PACKET_CODEC)
.build();

public static void register() {
Registry.register(Registries.DATA_COMPONENT_TYPE, Resources.id("cable_source"), CableSource);
}
}
40 changes: 11 additions & 29 deletions projects/pswg/src/main/java/com/parzivail/pswg/item/CableItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import com.parzivail.pswg.blockentity.PowerCouplingBlockEntity;
import com.parzivail.pswg.container.SwgBlocks;
import com.parzivail.pswg.container.SwgComponents;
import com.parzivail.util.client.TooltipUtil;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;

import java.util.List;

Expand All @@ -24,25 +23,18 @@ public CableItem(Item.Settings settings)
@Override
public boolean hasGlint(ItemStack stack)
{
var nbt = stack.getOrCreateNbt();
return nbt.contains("source");
return stack.contains(SwgComponents.CableSource);
}

@Override
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type)
{
super.appendTooltip(stack, context, tooltip, type);

var nbt = stack.getOrCreateNbt();
if (nbt.contains("source"))
var source = stack.get(SwgComponents.CableSource);
if (source != null)
{
var source = nbt.getCompound("source");

var sourceX = source.getInt("x");
var sourceY = source.getInt("y");
var sourceZ = source.getInt("z");

tooltip.add(TooltipUtil.getStatus(this, sourceX, sourceY, sourceZ));
tooltip.add(TooltipUtil.getStatus(this, source.getX(), source.getY(), source.getZ()));
}
}

Expand All @@ -57,28 +49,18 @@ public ActionResult useOnBlock(ItemUsageContext context)
var playerEntity = context.getPlayer();
if (!world.isClient && playerEntity != null)
{
var nbt = context.getStack().getOrCreateNbt();
if (nbt.contains("source"))
var source = context.getStack().get(SwgComponents.CableSource);
if (source != null)
{
var source = nbt.getCompound("source");

var sourceX = source.getInt("x");
var sourceY = source.getInt("y");
var sourceZ = source.getInt("z");
var sourcePos = new BlockPos(sourceX, sourceY, sourceZ);

if (!blockPos.equals(sourcePos) && world.getBlockEntity(sourcePos) instanceof PowerCouplingBlockEntity pcbe)
if (!blockPos.equals(source) && world.getBlockEntity(source) instanceof PowerCouplingBlockEntity pcbe)
pcbe.attachTo(world, blockPos);

nbt.remove("source");
context.getStack().remove(SwgComponents.CableSource);
}
else
{
var e = new NbtCompound();
e.putInt("x", blockPos.getX());
e.putInt("y", blockPos.getY());
e.putInt("z", blockPos.getZ());
nbt.put("source", e);
source = blockPos;
context.getStack().set(SwgComponents.CableSource, source);
}
}

Expand Down

0 comments on commit 52c5b3e

Please sign in to comment.