Skip to content

Commit

Permalink
feat: network card
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Nov 1, 2023
1 parent dc0224f commit 60c0863
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Configuration Card. It copies device configurations and can transfer upgrades.
- Network Receiver
- Network Card

## [2.0.0-milestone.3.1] - 2023-10-30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.refinedmods.refinedstorage2.platform.common.importer.ImporterContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.misc.ProcessorItem;
import com.refinedmods.refinedstorage2.platform.common.misc.WrenchItem;
import com.refinedmods.refinedstorage2.platform.common.networking.NetworkCardItem;
import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.StorageTypes;
Expand Down Expand Up @@ -330,6 +331,7 @@ private void registerSimpleItems(final RegistryCallback<Item> callback) {
ContentIds.CONFIGURATION_CARD,
ConfigurationCardItem::new
));
Items.INSTANCE.setNetworkCard(callback.register(ContentIds.NETWORK_CARD, NetworkCardItem::new));
}

private void registerProcessor(final RegistryCallback<Item> callback, final ProcessorItem.Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class ContentIds {
public static final ResourceLocation STORAGE_MONITOR = createIdentifier("storage_monitor");
public static final ResourceLocation CONFIGURATION_CARD = createIdentifier("configuration_card");
public static final ResourceLocation NETWORK_RECEIVER = createIdentifier("network_receiver");
public static final ResourceLocation NETWORK_CARD = createIdentifier("network_card");

private ContentIds() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@ private static void appendItems(final Consumer<ItemStack> consumer) {
consumer.accept(Items.INSTANCE.getWirelessGrid().createAtEnergyCapacity());
itemConsumer.accept(Items.INSTANCE.getCreativeWirelessGrid());
itemConsumer.accept(Items.INSTANCE.getConfigurationCard());
itemConsumer.accept(Items.INSTANCE.getNetworkCard());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public final class Items {
private Supplier<WirelessGridItem> creativeWirelessGrid;
@Nullable
private Supplier<Item> configurationCard;
@Nullable
private Supplier<Item> networkCard;

private Items() {
}
Expand Down Expand Up @@ -372,4 +374,12 @@ public void addNetworkReceiver(final Supplier<BlockItem> supplier) {
public List<Supplier<BlockItem>> getNetworkReceivers() {
return Collections.unmodifiableList(allNetworkReceivers);
}

public Item getNetworkCard() {
return Objects.requireNonNull(networkCard).get();
}

public void setNetworkCard(final Supplier<Item> supplier) {
this.networkCard = supplier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.refinedmods.refinedstorage2.platform.common.networking;

import com.refinedmods.refinedstorage2.platform.api.support.HelpTooltipComponent;

import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
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.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation;

// TODO: better active texture.
public class NetworkCardItem extends Item {
private static final MutableComponent UNBOUND_HELP = createTranslation("item", "network_card.unbound_help");
private static final MutableComponent BOUND_HELP = createTranslation("item", "network_card.bound_help");

private static final MutableComponent UNBOUND = createTranslation("item", "network_card.unbound")
.withStyle(ChatFormatting.RED);

private static final String TAG_POS = "pos";
private static final String TAG_DIMENSION = "dim";

public NetworkCardItem() {
super(new Item.Properties().stacksTo(1));
}

@Override
public InteractionResult useOn(final UseOnContext ctx) {
if (ctx.getLevel().isClientSide() || ctx.getPlayer() == null) {
return InteractionResult.CONSUME;
}
final BlockPos pos = ctx.getClickedPos();
final BlockState blockState = ctx.getLevel().getBlockState(pos);
if (!(blockState.getBlock() instanceof NetworkReceiverBlock)) {
return InteractionResult.CONSUME;
}
final CompoundTag tag = new CompoundTag();
tag.putLong(TAG_POS, pos.asLong());
tag.putString(TAG_DIMENSION, ctx.getLevel().dimension().location().toString());
ctx.getItemInHand().setTag(tag);
ctx.getPlayer().sendSystemMessage(createTranslation(
"item",
"network_card.bound",
pos.getX(),
pos.getY(),
pos.getZ()
));
return InteractionResult.SUCCESS;
}

@Override
public InteractionResultHolder<ItemStack> use(final Level level, final Player player, final InteractionHand hand) {
if (player.isCrouching()) {
if (!level.isClientSide()) {
player.sendSystemMessage(createTranslation("item", "network_card.unbound"));
}
return new InteractionResultHolder<>(InteractionResult.CONSUME, new ItemStack(this));
}
return super.use(level, player, hand);
}

@Override
public void appendHoverText(final ItemStack stack,
@Nullable final Level level,
final List<Component> lines,
final TooltipFlag flag) {
super.appendHoverText(stack, level, lines, flag);
if (!isActive(stack)) {
lines.add(UNBOUND);
return;
}
final BlockPos pos = getPosition(stack);
if (pos == null) {
return;
}
lines.add(createTranslation(
"item",
"network_card.bound",
pos.getX(),
pos.getY(),
pos.getZ()
).withStyle(ChatFormatting.GRAY));
}

@Nullable
public BlockPos getPosition(final ItemStack stack) {
if (stack.getTag() == null) {
return null;
}
return BlockPos.of(stack.getTag().getLong(TAG_POS));
}

@Override
public Optional<TooltipComponent> getTooltipImage(final ItemStack stack) {
return Optional.of(new HelpTooltipComponent(isActive(stack) ? BOUND_HELP : UNBOUND_HELP));
}

public boolean isActive(final ItemStack stack) {
return stack.getTag() != null && stack.getTag().contains(TAG_POS) && stack.getTag().contains(TAG_DIMENSION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.refinedmods.refinedstorage2.platform.common.networking;

import javax.annotation.Nullable;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;

public class NetworkCardItemPropertyFunction implements ClampedItemPropertyFunction {
public static final ResourceLocation NAME = new ResourceLocation("active");

@Override
public float unclampedCall(final ItemStack itemStack,
@Nullable final ClientLevel clientLevel,
@Nullable final LivingEntity livingEntity,
final int i) {
if (itemStack.getItem() instanceof NetworkCardItem cardItem) {
return cardItem.isActive(itemStack) ? 1 : 0;
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@
"item.refinedstorage2.configuration_card.applied_configuration": "Applied configuration.",
"item.refinedstorage2.configuration_card.empty_help": "Use on a storage network device while crouching to copy its configuration and upgrades to the card.",
"item.refinedstorage2.configuration_card.configured_help": "Use on the destination storage network device while crouching to transfer the configuration and upgrades. Use while crouching to clear.",
"item.refinedstorage2.network_card": "Network Card",
"item.refinedstorage2.network_card.unbound_help": "Use while crouching on a Network Receiver.",
"item.refinedstorage2.network_card.unbound": "Unbound.",
"item.refinedstorage2.network_card.bound_help": "Insert into a Network Transmitter. Use while crouching to clear binding.",
"item.refinedstorage2.network_card.bound": "Bound to %d, %d, %d.",
"misc.refinedstorage2.stored": "Stored: %s",
"misc.refinedstorage2.stored_with_capacity": "Stored: %s / %s (%d%%)",
"misc.refinedstorage2.total": "%d total",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"parent": "item/generated",
"overrides": [
{
"predicate": {
"active": 0
},
"model": "refinedstorage2:item/network_card/inactive"
},
{
"predicate": {
"active": 1
},
"model": "refinedstorage2:item/network_card/active"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "refinedstorage2:item/network_card/active"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "refinedstorage2:item/network_card/inactive"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"EEE",
"PAP",
"EEE"
],
"key": {
"E": {
"item": "refinedstorage2:quartz_enriched_iron"
},
"P": {
"item": "minecraft:paper"
},
"A": {
"item": "refinedstorage2:advanced_processor"
}
},
"result": {
"item": "refinedstorage2:network_card"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.Items;
import com.refinedmods.refinedstorage2.platform.common.content.KeyMappings;
import com.refinedmods.refinedstorage2.platform.common.controller.ControllerModelPredicateProvider;
import com.refinedmods.refinedstorage2.platform.common.networking.NetworkCardItemPropertyFunction;
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.StorageMonitorBlockEntityRenderer;
import com.refinedmods.refinedstorage2.platform.common.support.networkbounditem.NetworkBoundItemItemPropertyFunction;
import com.refinedmods.refinedstorage2.platform.common.support.tooltip.CompositeClientTooltipComponent;
Expand Down Expand Up @@ -367,5 +368,10 @@ private void registerItemProperties() {
ConfigurationCardItemPropertyFunction.NAME,
new ConfigurationCardItemPropertyFunction()
);
ItemProperties.register(
Items.INSTANCE.getNetworkCard(),
NetworkCardItemPropertyFunction.NAME,
new NetworkCardItemPropertyFunction()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.Items;
import com.refinedmods.refinedstorage2.platform.common.content.KeyMappings;
import com.refinedmods.refinedstorage2.platform.common.controller.ControllerModelPredicateProvider;
import com.refinedmods.refinedstorage2.platform.common.networking.NetworkCardItemPropertyFunction;
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.StorageMonitorBlockEntityRenderer;
import com.refinedmods.refinedstorage2.platform.common.support.networkbounditem.NetworkBoundItemItemPropertyFunction;
import com.refinedmods.refinedstorage2.platform.common.support.tooltip.CompositeClientTooltipComponent;
Expand Down Expand Up @@ -222,5 +223,10 @@ private static void registerItemProperties() {
ConfigurationCardItemPropertyFunction.NAME,
new ConfigurationCardItemPropertyFunction()
);
ItemProperties.register(
Items.INSTANCE.getNetworkCard(),
NetworkCardItemPropertyFunction.NAME,
new NetworkCardItemPropertyFunction()
);
}
}

0 comments on commit 60c0863

Please sign in to comment.