Skip to content
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
32 changes: 12 additions & 20 deletions src/main/java/com/ldtteam/structurize/client/gui/WindowScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import com.ldtteam.structurize.blockentities.interfaces.IBlueprintDataProviderBE;
import com.ldtteam.structurize.client.gui.util.InputFilters;
import com.ldtteam.structurize.client.gui.util.ItemPositionsStorage;
import com.ldtteam.structurize.client.rendertask.RenderTaskManager;
import com.ldtteam.structurize.client.rendertask.tasks.BoxPreviewData;
import com.ldtteam.structurize.client.rendertask.tasks.BoxPreviewRenderTask;
import com.ldtteam.structurize.network.messages.*;
import com.ldtteam.structurize.placement.SimplePlacementContext;
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
import com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers;
import com.ldtteam.structurize.storage.rendering.RenderingCache;
import com.ldtteam.structurize.storage.rendering.types.BoxPreviewData;
import com.ldtteam.structurize.util.PlacementSettings;
import com.ldtteam.structurize.util.ScanToolData;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
Expand Down Expand Up @@ -313,7 +314,7 @@ public void onOpened()
@Override
public void onClosed()
{
if (RenderingCache.getBoxPreviewData("scan") != null) // not confirmed/cancelled
if (RenderTaskManager.getTasksByGroup("scan") != null) // not confirmed/cancelled
{
updateBounds();
}
Expand Down Expand Up @@ -341,16 +342,8 @@ public void onUpdate()
*/
private void discardClicked()
{
RenderingCache.removeBox("scan");

for (Iterator<Map.Entry<String, BoxPreviewData>> iterator = RenderingCache.boxRenderingCache.entrySet().iterator(); iterator.hasNext(); )
{
final var entry = iterator.next();
if (entry.getKey().contains("clickedResource"))
{
iterator.remove();
}
}
RenderTaskManager.removeTaskGroup("scan");
RenderTaskManager.removeTaskGroup("clickedResource");
close();
}

Expand All @@ -363,7 +356,7 @@ private void confirmClicked()

final ScanToolData.Slot slot = data.getCurrentSlotData();
Network.getNetwork().sendToServer(new ScanOnServerMessage(slot, true));
RenderingCache.removeBox("scan");
RenderTaskManager.removeTaskGroup("scan");
close();
}

Expand Down Expand Up @@ -395,7 +388,7 @@ private void loadSlot()
pos2y.setText(String.valueOf(slot.getBox().getPos2().getY()));
pos2z.setText(String.valueOf(slot.getBox().getPos2().getZ()));

RenderingCache.queue("scan", slot.getBox());
RenderTaskManager.addRenderTask("scan", new BoxPreviewRenderTask("scan", slot.getBox(), 60 * 10));

findPaneOfTypeByID(NAME_LABEL, TextField.class).setText("");
if (!slot.getName().isEmpty())
Expand Down Expand Up @@ -438,8 +431,7 @@ private void updateBounds()
final String name = findPaneOfTypeByID(NAME_LABEL, TextField.class).getText();
final ScanToolData.Slot slot = data.getCurrentSlotData();
data.setCurrentSlotData(new ScanToolData.Slot(name, new BoxPreviewData(pos1, pos2, slot.getBox().getAnchor())));

RenderingCache.queue("scan", slot.getBox());
RenderTaskManager.addRenderTask("scan", new BoxPreviewRenderTask("scan", data.getCurrentSlotData().getBox(), 60 * 10));
Network.getNetwork().sendToServer(new UpdateScanToolMessage(data));
}

Expand Down Expand Up @@ -686,9 +678,9 @@ private void doHighLightBlocks(Button button, final ItemStorage block)
final ItemPositionsStorage itemPositionsStorage = allResources.get(block);
for (final BlockPos position : itemPositionsStorage.positions)
{
BoxPreviewData previewData = new BoxPreviewData(position, position, Optional.empty());
previewData.setExpireTime(30);
RenderingCache.queue("clickedResource" + position.toShortString(), previewData);
BoxPreviewRenderTask previewData =
new BoxPreviewRenderTask("clickedResource" + position.toShortString(), new BoxPreviewData(position, position, Optional.empty()), 30);
RenderTaskManager.addRenderTask("clickedResource", previewData);
}
window.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.ldtteam.structurize.client.rendercontext;

import com.ldtteam.structurize.client.rendertask.RenderTaskManager;
import com.ldtteam.structurize.client.rendertask.util.WorldRenderMacros;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.MultiBufferSource.BufferSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;

/**
* Main class for handling world rendering.
* Also holds all possible values which may be needed during rendering.
*/
public class WorldEventRenderContext
{
public static final WorldEventRenderContext INSTANCE = new WorldEventRenderContext();

private WorldEventRenderContext()
{
// singleton
}

public RenderLevelStageEvent stageEvent;
public BufferSource bufferSource;
public PoseStack poseStack;
public float partialTicks;
public ClientLevel clientLevel;
public LocalPlayer clientPlayer;
public ItemStack mainHandItem;

/**
* In chunks
*/
int clientRenderDist;

public void renderWorldLastEvent(final RenderLevelStageEvent event)
{
stageEvent = event;
bufferSource = WorldRenderMacros.getBufferSource();
poseStack = event.getPoseStack();
partialTicks = event.getPartialTick();
clientLevel = Minecraft.getInstance().level;
clientPlayer = Minecraft.getInstance().player;
mainHandItem = clientPlayer.getMainHandItem();
clientRenderDist = Minecraft.getInstance().options.renderDistance().get();

final Vec3 cameraPos = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();
poseStack.pushPose();
poseStack.translate(-cameraPos.x(), -cameraPos.y(), -cameraPos.z());

runRenderTasks(event);

bufferSource.endBatch();

poseStack.popPose();
}

private void runRenderTasks(final RenderLevelStageEvent event)
{
RenderTaskManager.render(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.ldtteam.structurize.client.rendertask;

import com.ldtteam.structurize.client.rendercontext.WorldEventRenderContext;
import com.ldtteam.structurize.client.rendertask.task.IRenderTask;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class RenderTaskManager
{
// Uses:
// Render a box with: Text, color, duration, transparency, infront/behind blocks
// Render text at a position(box optional?)
// Render a large box in world(claim borders, lumberjack etc)
// Timed task: enables/disables citizen glowing effect with duration

/**
* A position to highlight with a group and key.
*/
private static final Map<String, Map<String, IRenderTask>> RENDER_TASKS = new LinkedHashMap<>();

/**
* Highlights positions
*
* @param context rendering context
*/
public static void render(final WorldEventRenderContext context)
{
if (RENDER_TASKS.isEmpty())
{
return;
}

for (final Iterator<Map<String, IRenderTask>> groups = RENDER_TASKS.values().iterator(); groups.hasNext(); )
{
final Map<String, IRenderTask> group = groups.next();
for (final Iterator<IRenderTask> renderTaskIterator = group.values().iterator(); renderTaskIterator.hasNext(); )
{
final IRenderTask renderTask = renderTaskIterator.next();

if (renderTask.shouldRenderIn(context.stageEvent.getStage()))
{
renderTask.render(context);
}
}
}
}

/**
* Client tick callback
*/
public static void onClientTick()
{
for (final Iterator<Map<String, IRenderTask>> groups = RENDER_TASKS.values().iterator(); groups.hasNext(); )
{
final Map<String, IRenderTask> group = groups.next();
for (final Iterator<IRenderTask> containers = group.values().iterator(); containers.hasNext(); )
{
final IRenderTask renderDataContainer = containers.next();
boolean isDone = renderDataContainer.tick();
if (isDone)
{
containers.remove();
}
}

if (group.isEmpty())
{
groups.remove();
}
}
}

/**
* Clears all highlight items for the given group key.
*
* @param key the key to remove the render data for.
*/
public static void clearHighlightsForKey(final String key)
{
RENDER_TASKS.remove(key);
}

/**
* Adds a highlight item for the given key.
*
* @param key the group key of the item to render.
* @return the previous entry or null
*/
public static IRenderTask addRenderTask(final String key, final IRenderTask task)
{
return RENDER_TASKS.computeIfAbsent(key, k -> new LinkedHashMap<>()).put(task.id(), task);
}

@Nullable
public static Map<String, IRenderTask> getTasksByGroup(final String groupID)
{
return RENDER_TASKS.get(groupID);
}

public static boolean removeTaskGroup(final String groupID)
{
return RENDER_TASKS.remove(groupID) != null;
}

public static boolean removeTaskEntry(final String groupID, final String taskID)
{
Map<String, IRenderTask> entry = RENDER_TASKS.get(groupID);
if (entry != null)
{
return entry.remove(taskID) != null;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ldtteam.structurize.client.rendertask.task;

public interface IClientTask extends ITickingTask
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ldtteam.structurize.client.rendertask.task;

import com.ldtteam.structurize.client.rendercontext.WorldEventRenderContext;
import net.minecraftforge.client.event.RenderLevelStageEvent;

public interface IRenderTask extends IClientTask
{
/**
* Indicate the render data it should continue rendering.
*/
void render(final WorldEventRenderContext context);

/**
* Precheck for the render stage, as render(WorldEventRenderContext) is run for all stages
*
* @return true if rendering active for this stage
*/
boolean shouldRenderIn(RenderLevelStageEvent.Stage renderStage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ldtteam.structurize.client.rendertask.task;

public interface ITickingTask
{
/**
* Client Tick callback
*
* @return true if task finished, false if not
*/
public boolean tick();

/**
* Task string identifier
*
* @return
*/
public String id();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ldtteam.structurize.client.rendertask.task;

public abstract class TimedTask implements IClientTask
{
private final String id;
private int ticksLeft = 0;

protected TimedTask(final String id, final int seconds)
{
this.id = id;
this.ticksLeft = seconds * 20;
}

/**
* Duration of the task
*/
public void setDurationSeconds(final int seconds)
{
this.ticksLeft = seconds * 20;
}

public boolean isExpired()
{
return ticksLeft <= 0;
}

@Override
public boolean tick()
{
ticksLeft--;
return isExpired();
}

@Override
public String id()
{
return id;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ldtteam.structurize.storage.rendering.types;
package com.ldtteam.structurize.client.rendertask.tasks;

import net.minecraft.core.BlockPos;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,8 +19,6 @@ public class BoxPreviewData
@NotNull
private Optional<BlockPos> anchor;

private long expireTime = Long.MAX_VALUE;

/**
* Create a new box.
* @param pos1 the first pos.
Expand Down Expand Up @@ -53,14 +51,4 @@ public void setAnchor(final Optional<BlockPos> anchor)
{
this.anchor = anchor;
}

public boolean isExpired()
{
return System.currentTimeMillis() - expireTime > 0;
}

public void setExpireTime(final int seconds)
{
expireTime = System.currentTimeMillis() + seconds * 1000;
}
}
Loading
Loading