Skip to content

Commit

Permalink
feat: starting crafting tasks on the server
Browse files Browse the repository at this point in the history
Not autocrafting itself yet ;)
  • Loading branch information
raoulvdberge committed Oct 4, 2024
1 parent 673817f commit 1b3a43e
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public void openCraftingPreview(final List<ResourceAmount> requests) {
minecraft.setScreen(new AutocraftingPreviewScreen(
minecraft.screen,
inventory,
requests.stream().map(AutocraftingRequest::of).toList()
requests.stream().map(AutocraftingRequest::of).collect(Collectors.toList())
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.refinedmods.refinedstorage.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerImpl;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;
Expand All @@ -33,7 +32,7 @@ public class AutocraftingPreviewContainerMenu extends AbstractResourceContainerM
48,
ResourceSlotType.FILTER
));
this.requests = Collections.unmodifiableList(requests);
this.requests = requests;
this.currentRequest = requests.getFirst();
}

Expand Down Expand Up @@ -62,16 +61,35 @@ void amountChanged(final double amount) {
}
}

public void previewReceived(final UUID id, final AutocraftingPreview preview) {
public void previewResponseReceived(final UUID id, final AutocraftingPreview preview) {
if (currentRequest.previewReceived(id, preview) && listener != null) {
listener.previewChanged(preview);
}
}

public void loadCurrentRequest() {
void loadCurrentRequest() {
if (listener != null) {
currentRequest.clearPreview();
listener.requestChanged(currentRequest);
}
}

void startRequest(final double amount) {
currentRequest.start(amount);
}

boolean requestStarted(final UUID id) {
if (currentRequest.isStarted(id)) {
if (listener != null) {
listener.requestRemoved(currentRequest);
}
requests.remove(currentRequest);
if (!requests.isEmpty()) {
setCurrentRequest(requests.getFirst());
return false;
}
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ interface AutocraftingPreviewListener {
void requestChanged(AutocraftingRequest request);

void previewChanged(@Nullable AutocraftingPreview preview);

void requestRemoved(AutocraftingRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;

@FunctionalInterface
public interface AutocraftingPreviewProvider {
AutocraftingPreview getPreview(PlatformResourceKey resource, long amount);

boolean start(PlatformResourceKey resource, long amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

import com.google.common.util.concurrent.RateLimiter;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class AutocraftingPreviewScreen extends AbstractAmountScreen<Autocrafting
private final List<AutocraftingRequestButton> requestButtons = new ArrayList<>();
private final boolean requestsButtonsVisible;

private final RateLimiter requestRateLimiter = RateLimiter.create(0.5);
private final RateLimiter requestRateLimiter = RateLimiter.create(1);

@Nullable
private Double changedAmount;
Expand Down Expand Up @@ -111,10 +112,7 @@ protected void init() {
initRequestButtons();
}
if (confirmButton != null) {
confirmButton.active = false;
confirmButton.setMessage(START);
confirmButton.setTooltip(null);
confirmButton.setError(false);
setStartDisabled();
}
getMenu().loadCurrentRequest();
getExclusionZones().add(new Rect2i(
Expand Down Expand Up @@ -144,12 +142,7 @@ private void initRequestButtons() {
requestButton.visible = isCraftingRequestButtonVisible(y);
}
});
final int totalRequestButtons = getMenu().getRequests().size() - REQUESTS_VISIBLE;
final int maxOffset = requestButtonsScrollbar.isSmoothScrolling()
? totalRequestButtons * REQUEST_BUTTON_HEIGHT
: totalRequestButtons;
requestButtonsScrollbar.setEnabled(maxOffset > 0);
requestButtonsScrollbar.setMaxOffset(maxOffset);
updateRequestsScrollbar();
for (int i = 0; i < getMenu().getRequests().size(); ++i) {
final AutocraftingRequest request = getMenu().getRequests().get(i);
final int buttonY = getCraftingRequestButtonY(i);
Expand Down Expand Up @@ -189,19 +182,17 @@ private void setPreview(@Nullable final AutocraftingPreview preview) {
if (previewItemsScrollbar == null || confirmButton == null) {
return;
}
confirmButton.setMessage(START);
if (preview == null) {
previewItemsScrollbar.setEnabled(false);
previewItemsScrollbar.setMaxOffset(0);
confirmButton.active = false;
confirmButton.setError(false);
confirmButton.setTooltip(null);
setStartDisabled();
return;
}
final int items = preview.items().size();
final int rows = Math.ceilDiv(items, COLUMNS) - ROWS_VISIBLE;
previewItemsScrollbar.setMaxOffset(previewItemsScrollbar.isSmoothScrolling() ? rows * ROW_HEIGHT : rows);
previewItemsScrollbar.setEnabled(rows > 0);
confirmButton.setMessage(START);
confirmButton.active = preview.type() == AutocraftingPreviewType.SUCCESS;
confirmButton.setError(preview.type() != AutocraftingPreviewType.SUCCESS);
confirmButton.setTooltip(preview.type() == AutocraftingPreviewType.MISSING_RESOURCES
Expand Down Expand Up @@ -406,14 +397,28 @@ protected void onAmountFieldChanged() {
if (amountField == null || confirmButton == null) {
return;
}
getAndValidateAmount().ifPresentOrElse(amount -> {
setPending();
changedAmount = amount;
amountField.setTextColor(0xFFFFFF);
}, () -> {
setStartDisabled();
amountField.setTextColor(0xFF5555);
});
}

private void setPending() {
confirmButton.active = false;
confirmButton.setError(false);
confirmButton.setTooltip(null);
confirmButton.setMessage(PENDING);
getAndValidateAmount().ifPresentOrElse(amount -> {
changedAmount = amount;
amountField.setTextColor(0xFFFFFF);
}, () -> amountField.setTextColor(0xFF5555));
}

private void setStartDisabled() {
confirmButton.active = false;
confirmButton.setError(false);
confirmButton.setTooltip(null);
confirmButton.setMessage(START);
}

@Override
Expand All @@ -432,6 +437,8 @@ protected void reset() {

@Override
protected boolean confirm(final Double amount) {
setPending();
getMenu().startRequest(amount);
return false;
}

Expand All @@ -444,4 +451,34 @@ public void requestChanged(final AutocraftingRequest request) {
public void previewChanged(@Nullable final AutocraftingPreview preview) {
setPreview(preview);
}

@Override
public void requestRemoved(final AutocraftingRequest request) {
requestButtons.removeIf(requestButton -> requestButton.getRequest() == request);
updateRequestsScrollbar();
for (int i = 0; i < requestButtons.size(); ++i) {
final AutocraftingRequestButton requestButton = requestButtons.get(i);
final int buttonY = getCraftingRequestButtonY(i);
requestButton.setY(buttonY);
requestButton.visible = isCraftingRequestButtonVisible(buttonY);
}
}

private void updateRequestsScrollbar() {
if (requestButtonsScrollbar == null) {
return;
}
final int totalRequestButtons = getMenu().getRequests().size() - REQUESTS_VISIBLE;
final int maxOffset = requestButtonsScrollbar.isSmoothScrolling()
? totalRequestButtons * REQUEST_BUTTON_HEIGHT
: totalRequestButtons;
requestButtonsScrollbar.setEnabled(maxOffset > 0);
requestButtonsScrollbar.setMaxOffset(maxOffset);
}

public void responseReceived(final UUID id, final boolean started) {
if (started && getMenu().requestStarted(id)) {
tryCloseToParent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ boolean trySendRequest(final double amountRequested) {
return true;
}

void start(final double amountRequested) {
if (!(resource instanceof PlatformResourceKey resourceKey)) {
return;
}
final long normalizedAmount = resourceKey.getResourceType().normalizeAmount(amountRequested);
C2SPackets.sendAutocraftingRequest(id, resourceKey, normalizedAmount);
}

ResourceKey getResource() {
return resource;
}
Expand All @@ -69,4 +77,8 @@ void clearPreview() {
pendingPreviewAmount = 0;
preview = null;
}

boolean isStarted(final UUID startedId) {
return id.equals(startedId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ public AutocraftingPreview getPreview(final PlatformResourceKey resource, final
: AutocraftingPreviewType.SUCCESS, items);
}

@Override
public boolean start(final PlatformResourceKey resource, final long amount) {
return true;
}

public boolean isLargeSlot(final Slot slot) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private static boolean tryStartAutocrafting(final PlatformGridResource resource)
requests.add(new ResourceAmount(request.resource(), request.amount() * 2));
requests.add(new ResourceAmount(request.resource(), request.amount() * 3));
requests.add(new ResourceAmount(request.resource(), request.amount() * 4));
requests.add(new ResourceAmount(request.resource(), request.amount() * 5));
requests.add(new ResourceAmount(request.resource(), request.amount() * 4));
}
RefinedStorageApi.INSTANCE.openCraftingPreview(requests);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private void tryConfirmAndCloseToParent() {
});
}

private boolean tryCloseToParent() {
protected final boolean tryCloseToParent() {
if (parent != null) {
Minecraft.getInstance().setScreen(parent);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.refinedmods.refinedstorage.common.support.packet.c2s;

import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingPreviewProvider;
import com.refinedmods.refinedstorage.common.support.packet.PacketContext;
import com.refinedmods.refinedstorage.common.support.packet.s2c.S2CPackets;
import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs;

import java.util.UUID;

import net.minecraft.core.UUIDUtil;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.level.ServerPlayer;

import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;

public record AutocraftingRequestPacket(UUID id,
PlatformResourceKey resource,
long amount) implements CustomPacketPayload {
public static final Type<AutocraftingRequestPacket> PACKET_TYPE = new Type<>(
createIdentifier("autocrafting_request")
);
public static final StreamCodec<RegistryFriendlyByteBuf, AutocraftingRequestPacket> STREAM_CODEC =
StreamCodec
.composite(
UUIDUtil.STREAM_CODEC, AutocraftingRequestPacket::id,
ResourceCodecs.STREAM_CODEC, AutocraftingRequestPacket::resource,
ByteBufCodecs.VAR_LONG, AutocraftingRequestPacket::amount,
AutocraftingRequestPacket::new
);

public static void handle(final AutocraftingRequestPacket packet, final PacketContext ctx) {
if (ctx.getPlayer().containerMenu instanceof AutocraftingPreviewProvider provider) {
final boolean started = provider.start(packet.resource(), packet.amount());
S2CPackets.sendAutocraftingResponse((ServerPlayer) ctx.getPlayer(), packet.id, started);
}
}

@Override
public Type<? extends CustomPacketPayload> type() {
return PACKET_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,10 @@ public static void sendAutocraftingPreviewRequest(final UUID id,
final long amount) {
Platform.INSTANCE.sendPacketToServer(new AutocraftingPreviewRequestPacket(id, resource, amount));
}

public static void sendAutocraftingRequest(final UUID id,
final PlatformResourceKey resource,
final long amount) {
Platform.INSTANCE.sendPacketToServer(new AutocraftingRequestPacket(id, resource, amount));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.refinedmods.refinedstorage.common.support.packet.s2c;

import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingPreview;
import com.refinedmods.refinedstorage.common.support.packet.PacketContext;
import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil;

import java.util.UUID;
Expand All @@ -26,7 +25,7 @@ public record AutocraftingPreviewResponsePacket(UUID id, AutocraftingPreview pre
);

public static void handle(final AutocraftingPreviewResponsePacket packet) {
ClientPlatformUtil.craftingPreviewReceived(packet.id, packet.preview);
ClientPlatformUtil.autocraftingPreviewResponseReceived(packet.id, packet.preview);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.refinedmods.refinedstorage.common.support.packet.s2c;

import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil;

import java.util.UUID;

import net.minecraft.core.UUIDUtil;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;

import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;

public record AutocraftingResponsePacket(UUID id, boolean started) implements CustomPacketPayload {
public static final Type<AutocraftingResponsePacket> PACKET_TYPE = new Type<>(
createIdentifier("autocrafting_response")
);

public static final StreamCodec<RegistryFriendlyByteBuf, AutocraftingResponsePacket> STREAM_CODEC =
StreamCodec.composite(
UUIDUtil.STREAM_CODEC, AutocraftingResponsePacket::id,
ByteBufCodecs.BOOL, AutocraftingResponsePacket::started,
AutocraftingResponsePacket::new
);

public static void handle(final AutocraftingResponsePacket packet) {
ClientPlatformUtil.autocraftingResponseReceived(packet.id, packet.started);
}

@Override
public Type<? extends CustomPacketPayload> type() {
return PACKET_TYPE;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ public static void sendAutocraftingPreviewResponse(final ServerPlayer player,
final AutocraftingPreview preview) {
Platform.INSTANCE.sendPacketToClient(player, new AutocraftingPreviewResponsePacket(id, preview));
}

public static void sendAutocraftingResponse(final ServerPlayer player,
final UUID id,
final boolean started) {
Platform.INSTANCE.sendPacketToClient(player, new AutocraftingResponsePacket(id, started));
}
}
Loading

0 comments on commit 1b3a43e

Please sign in to comment.