Skip to content
Merged
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
Expand Up @@ -77,6 +77,7 @@ public class ItemWorldRenderer {
private static final int capabilityColor = FastColor.ARGB32.color(100, 100, 0, 255);
private static final int capabilityColorLimitedView = FastColor.ARGB32.color(100, 0, 100, 255);
private static final int cableColor = FastColor.ARGB32.color(100, 100, 255, 0);
private static final int noNetworkErrorColor = FastColor.ARGB32.color(200, 255, 50, 50);
private static final VBOCache vboCache = new VBOCache();

@SFMSubscribeEvent(value = SFMDist.CLIENT)
Expand Down Expand Up @@ -244,9 +245,19 @@ private static void handleNetworkTool(

RENDER_TYPE.setupRenderState();

drawVbo(VBOKind.NETWORK_TOOL_CABLES, poseStack, cablePositions, cableColor, event);
drawVbo(VBOKind.NETWORK_TOOL_CAPABILITIES, poseStack, capabilityPositions, capabilityColor, event);

var selectedPos = NetworkToolItem.getSelectedNetworkBlockPos(networkTool);
if (cablePositions.isEmpty() && selectedPos != null) {
drawVbo(
VBOKind.NETWORK_TOOL_CABLES,
poseStack,
BlockPosSet.of(selectedPos),
noNetworkErrorColor,
event
);
} else {
drawVbo(VBOKind.NETWORK_TOOL_CABLES, poseStack, cablePositions, cableColor, event);
drawVbo(VBOKind.NETWORK_TOOL_CAPABILITIES, poseStack, capabilityPositions, capabilityColor, event);
}
RENDER_TYPE.clearRenderState();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ca.teamdman.sfm.common.capability.SFMBlockCapabilityResult;
import ca.teamdman.sfm.common.logging.TranslatableLogger;
import ca.teamdman.sfm.common.util.*;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.ChunkPos;
Expand Down Expand Up @@ -149,12 +150,20 @@ public BlockPosIterator getCapabilityProviderPositions() {

return levelCapabilityCache.getPositions();
}
public LongSet getCapabilityProviderPositionsRaw() {
return levelCapabilityCache.getPositionsRaw();
}


public BlockPosIterator getCablePositions() {

return members().positions();
}

public LongSet getCablePositionsRaw() {
return members().keySet();
}

@Override
void purgeChunk(ChunkPos chunkPos) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ca.teamdman.sfm.common.capability.SFMBlockCapabilityResult;
import ca.teamdman.sfm.common.util.*;
import it.unimi.dsi.fastutil.longs.Long2ObjectFunction;
import it.unimi.dsi.fastutil.longs.LongSet;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -112,6 +113,10 @@ public BlockPosIterator getPositions() {
return blockPosToCapKindToDirectionToCapResultMap.positions();
}

public LongSet getPositionsRaw() {
return blockPosToCapKindToDirectionToCapResultMap.keySet();
}

public void remove(
BlockPos memberBlockPos,
SFMBlockCapabilityKind<?> capKind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.teamdman.sfm.common.item;

import ca.teamdman.sfm.client.handler.NetworkToolKeyMappingHandler;
import ca.teamdman.sfm.client.registry.SFMKeyMappings;
import ca.teamdman.sfm.common.block_network.CableNetwork;
import ca.teamdman.sfm.common.block_network.CableNetworkManager;
Expand All @@ -10,6 +11,7 @@
import ca.teamdman.sfm.common.util.BlockPosSet;
import ca.teamdman.sfm.common.util.CompressedBlockPosSet;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
Expand All @@ -36,14 +38,26 @@ public NetworkToolItem() {
@Override
public InteractionResult onItemUseFirst(
ItemStack stack,
UseOnContext pContext
UseOnContext ctx
) {

if (!pContext.getLevel().isClientSide) return InteractionResult.SUCCESS;
SFMPackets.sendToServer(new ServerboundNetworkToolUsePacket(
pContext.getClickedPos(),
pContext.getClickedFace()
));
var level = ctx.getLevel();
Player player = ctx.getPlayer();
if (level.isClientSide && player != null) {
boolean pickBlock = SFMKeyMappings.isKeyDown(SFMKeyMappings.TOGGLE_NETWORK_TOOL_OVERLAY_KEY);
ServerboundNetworkToolUsePacket msg = new ServerboundNetworkToolUsePacket(
ctx.getHand(),
ctx.getClickedPos(),
ctx.getClickedFace(),
pickBlock
);
SFMPackets.sendToServer(msg);
if (pickBlock) {
// we don't want to toggle the overlay if we're using pick-block
NetworkToolKeyMappingHandler.setExternalDebounce();
}
return InteractionResult.SUCCESS;
}
return InteractionResult.CONSUME;
}

Expand Down Expand Up @@ -83,46 +97,80 @@ public void inventoryTick(
if (!isInHand) return;
boolean shouldRefresh = pEntity.tickCount % 20 == 0;
if (!shouldRefresh) return;
regenerateCablePositions(pStack, pLevel, pPlayer);

final long maxDistance = 128;

// Get the networks in range
Stream<CableNetwork> networksInRange = CableNetworkManager
.getNetworksInRange(pLevel, pEntity.blockPosition(), maxDistance);
// Remove the data stored by older versions of the mod
pStack.getOrCreateTag().remove("networks");
}

// Get the positions of the cables and the capability providers from the networks
public static void regenerateCablePositions(
ItemStack pStack,
Level pLevel,
Player pPlayer
) {
// Initialize with default capacity
// We don't know how many *unique* positions we are going to see
BlockPosSet cablePositions = new BlockPosSet();
BlockPosSet capabilityProviderPositions = new BlockPosSet();
networksInRange
.forEach(network -> {
network.getCablePositions().forEach(cablePositions::add);
network.getCapabilityProviderPositions().forEach(capabilityProviderPositions::add);
});

// Update the network tool data
// Find the networks and track the positions
for (CableNetwork cableNetwork : (Iterable<CableNetwork>) getNetworksForOverlay(pStack, pLevel, pPlayer)::iterator) {
cablePositions.addAll(cableNetwork.getCablePositionsRaw());
capabilityProviderPositions.addAll(cableNetwork.getCapabilityProviderPositionsRaw());
}

// Update the item data
setCablePositions(pStack, cablePositions);
setCapabilityProviderPositions(pStack, capabilityProviderPositions);
}

// Remove the data stored by older versions of the mod
pStack.getOrCreateTag().remove("networks");
public static boolean getOverlayEnabled(ItemStack stack) {

return getOverlayMode(stack) != NetworkToolOverlayMode.HIDDEN;
}

/**
* Returns the current enum mode for the network tool item.
*/
public static NetworkToolOverlayMode getOverlayMode(ItemStack stack) {

public static boolean getOverlayEnabled(ItemStack stack) {
CompoundTag tag = stack.getOrCreateTag();
if (tag.contains("sfm:network_tool_overlay_disabled") && tag.getBoolean("sfm:network_tool_overlay_disabled")) {
return NetworkToolOverlayMode.HIDDEN;
}

return !stack.getOrCreateTag().getBoolean("sfm:network_tool_overlay_disabled");
int ordinal = tag.getInt("sfm:network_tool_overlay_mode");
// fallback if out of bounds or missing
if (ordinal < 0 || ordinal >= NetworkToolOverlayMode.values().length) {
return NetworkToolOverlayMode.SHOW_ALL;
}
return NetworkToolOverlayMode.values()[ordinal];
}

public static void cycleOverlayMode(ItemStack stack) {

NetworkToolOverlayMode current = getOverlayMode(stack);
NetworkToolOverlayMode newMode = current == NetworkToolOverlayMode.SHOW_ALL
? NetworkToolOverlayMode.HIDDEN
: NetworkToolOverlayMode.SHOW_ALL;
setOverlayMode(stack, newMode);
}

public static void setOverlayEnabled(
public static void setSelectedNetworkBlockPos(
ItemStack stack,
boolean value
BlockPos pos
) {

if (value) {
stack.getOrCreateTag().remove("sfm:network_tool_overlay_disabled");
} else {
stack.getOrCreateTag().putBoolean("sfm:network_tool_overlay_disabled", true);
}
setOverlayMode(stack, NetworkToolOverlayMode.SHOW_SELECTED_NETWORK);
stack.getOrCreateTag().put("sfm:selected_network_block_pos", NbtUtils.writeBlockPos(pos));
}

@Nullable
public static BlockPos getSelectedNetworkBlockPos(ItemStack stack) {

return stack.getOrCreateTag().contains("sfm:selected_network_block_pos")
? NbtUtils.readBlockPos(stack.getOrCreateTag().getCompound("sfm:selected_network_block_pos"))
: null;
}

public static void setCablePositions(
Expand Down Expand Up @@ -173,4 +221,46 @@ public static BlockPosSet getCapabilityProviderPositions(ItemStack stack) {
.collect(BlockPosSet.collector());
}

protected static Stream<CableNetwork> getNetworksForOverlay(
ItemStack pStack,
Level pLevel,
Player pPlayer
) {

final long maxDistance = 128;

BlockPos blockPos = getOverlayMode(pStack) == NetworkToolOverlayMode.SHOW_SELECTED_NETWORK
? getSelectedNetworkBlockPos(pStack)
: null;

if (blockPos != null) {
return CableNetworkManager.getOrRegisterNetworkFromCablePosition(pLevel, blockPos).stream();
} else {
return CableNetworkManager.getNetworksInRange(pLevel, pPlayer.blockPosition(), maxDistance);
}
}

/**
* Sets the view mode in NBT.
*/
protected static void setOverlayMode(
ItemStack stack,
NetworkToolOverlayMode mode
) {

stack.getOrCreateTag().putInt("sfm:network_tool_overlay_mode", mode.ordinal());
if (mode != NetworkToolOverlayMode.SHOW_SELECTED_NETWORK) {
stack.getOrCreateTag().remove("sfm:selected_network_block_pos");
}

// remove the data stored by older versions of the mod
stack.getOrCreateTag().remove("sfm:network_tool_overlay_disabled");
}

public enum NetworkToolOverlayMode {
SHOW_ALL,
SHOW_SELECTED_NETWORK,
HIDDEN
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void handle(
if (sender == null) return;
ItemStack networkToolItemStack = sender.getItemInHand(msg.hand);
if (networkToolItemStack.getItem() == SFMItems.NETWORK_TOOL_ITEM.get()) {
boolean active = NetworkToolItem.getOverlayEnabled(networkToolItemStack);
NetworkToolItem.setOverlayEnabled(networkToolItemStack, !active);
NetworkToolItem.cycleOverlayMode(networkToolItemStack);
NetworkToolItem.regenerateCablePositions(networkToolItemStack, sender.getLevel(), sender);
}
}

Expand Down
Loading