Fix crafting monitor to work with the new system

This commit is contained in:
Raoul Van den Berge
2016-08-29 21:20:45 +02:00
parent 4655d1f0d5
commit 9d71f8bb61
9 changed files with 77 additions and 25 deletions

View File

@@ -5,6 +5,8 @@ import net.minecraft.world.World;
import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.network.INetworkMaster;
import javax.annotation.Nullable;
/**
* Represents a crafting task.
*/
@@ -17,12 +19,13 @@ public interface ICraftingTask {
/**
* @return The child task
*/
@Nullable
ICraftingTask getChild();
/**
* @param child The child task
*/
void setChild(ICraftingTask child);
void setChild(@Nullable ICraftingTask child);
/**
* @param world The world

View File

@@ -52,6 +52,7 @@ public interface IItemGridHandler {
* Called when a player wants to cancel a crafting task.
*
* @param id The task ID, or -1 for all tasks
* @param child Whether to only cancel the child of this crafting task
*/
void onCraftingCancelRequested(int id);
void onCraftingCancelRequested(int id, boolean child);
}

View File

@@ -3,16 +3,19 @@ package refinedstorage.apiimpl.autocrafting.task;
import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.network.INetworkMaster;
import javax.annotation.Nullable;
public abstract class CraftingTask implements ICraftingTask {
protected ICraftingTask child;
@Override
@Nullable
public ICraftingTask getChild() {
return child;
}
@Override
public void setChild(ICraftingTask child) {
public void setChild(@Nullable ICraftingTask child) {
this.child = child;
}

View File

@@ -142,9 +142,16 @@ public class ItemGridHandler implements IItemGridHandler {
}
@Override
public void onCraftingCancelRequested(int id) {
public void onCraftingCancelRequested(int id, boolean child) {
if (id >= 0 && id < network.getCraftingTasks().size()) {
network.cancelCraftingTask(network.getCraftingTasks().get(id));
ICraftingTask task = network.getCraftingTasks().get(id);
if (child) {
task.getChild().onCancelled(network);
task.setChild(null);
} else {
network.cancelCraftingTask(task);
}
} else if (id == -1) {
for (ICraftingTask task : network.getCraftingTasks()) {
network.cancelCraftingTask(task);

View File

@@ -156,9 +156,11 @@ public class GuiCraftingMonitor extends GuiBase {
super.actionPerformed(button);
if (button == cancelButton && itemSelected != -1) {
RefinedStorage.INSTANCE.network.sendToServer(new MessageCraftingMonitorCancel(craftingMonitor, getTasks().get(itemSelected).getId()));
ClientCraftingTask task = getTasks().get(itemSelected);
RefinedStorage.INSTANCE.network.sendToServer(new MessageCraftingMonitorCancel(craftingMonitor, task.getId(), task.isChild()));
} else if (button == cancelAllButton && getTasks().size() > 0) {
RefinedStorage.INSTANCE.network.sendToServer(new MessageCraftingMonitorCancel(craftingMonitor, -1));
RefinedStorage.INSTANCE.network.sendToServer(new MessageCraftingMonitorCancel(craftingMonitor, -1, false));
}
}

View File

@@ -12,15 +12,17 @@ public class MessageCraftingMonitorCancel extends MessageHandlerPlayerToServer<M
private int y;
private int z;
private int id;
private boolean child;
public MessageCraftingMonitorCancel() {
}
public MessageCraftingMonitorCancel(TileCraftingMonitor craftingMonitor, int id) {
public MessageCraftingMonitorCancel(TileCraftingMonitor craftingMonitor, int id, boolean child) {
this.x = craftingMonitor.getPos().getX();
this.y = craftingMonitor.getPos().getY();
this.z = craftingMonitor.getPos().getZ();
this.id = id;
this.child = child;
}
@Override
@@ -29,6 +31,7 @@ public class MessageCraftingMonitorCancel extends MessageHandlerPlayerToServer<M
y = buf.readInt();
z = buf.readInt();
id = buf.readInt();
child = buf.readBoolean();
}
@Override
@@ -37,6 +40,7 @@ public class MessageCraftingMonitorCancel extends MessageHandlerPlayerToServer<M
buf.writeInt(y);
buf.writeInt(z);
buf.writeInt(id);
buf.writeBoolean(child);
}
@Override
@@ -47,7 +51,7 @@ public class MessageCraftingMonitorCancel extends MessageHandlerPlayerToServer<M
TileCraftingMonitor monitor = (TileCraftingMonitor) tile;
if (monitor.isConnected()) {
monitor.getNetwork().getItemGridHandler().onCraftingCancelRequested(message.id);
monitor.getNetwork().getItemGridHandler().onCraftingCancelRequested(message.id, message.child);
}
}
}

View File

@@ -1,24 +1,29 @@
package refinedstorage.tile;
import net.minecraft.item.ItemStack;
import refinedstorage.api.autocrafting.task.ICraftingTask;
public class ClientCraftingTask {
private ItemStack output;
private int id;
private boolean isChild;
private String status;
// Used server-side while sending
private ItemStack[] outputs;
private ClientCraftingTask child;
public ClientCraftingTask(ItemStack output, int id, String status) {
public ClientCraftingTask(ItemStack output, int id, String status, boolean isChild) {
this.output = output;
this.id = id;
this.status = status;
this.isChild = isChild;
}
public ClientCraftingTask(String status, ItemStack[] outputs) {
public ClientCraftingTask(String status, ItemStack[] outputs, ICraftingTask child) {
this.status = status;
this.outputs = outputs;
this.child = child != null ? new ClientCraftingTask(child.getStatus(), child.getPattern().getOutputs(), child.getChild()) : null;
}
public ItemStack getOutput() {
@@ -36,4 +41,12 @@ public class ClientCraftingTask {
public String getStatus() {
return status;
}
public ClientCraftingTask getChild() {
return child;
}
public boolean isChild() {
return isChild;
}
}

View File

@@ -17,7 +17,8 @@ public class TileCraftingMonitor extends TileNode {
if (tile.connected) {
List<ClientCraftingTask> tasks = tile.network.getCraftingTasks().stream().map(t -> new ClientCraftingTask(
t.getStatus(),
t.getPattern().getOutputs()
t.getPattern().getOutputs(),
t.getChild()
)).collect(Collectors.toList());
return tasks;

View File

@@ -53,6 +53,11 @@ public final class RefinedStorageSerializers {
buf.writeInt(tasks.size());
for (ClientCraftingTask task : tasks) {
writeTask(buf, task);
}
}
private void writeTask(PacketBuffer buf, ClientCraftingTask task) {
ByteBufUtils.writeUTF8String(buf, task.getStatus());
buf.writeInt(task.getOutputs().length);
@@ -60,6 +65,11 @@ public final class RefinedStorageSerializers {
for (ItemStack output : task.getOutputs()) {
ByteBufUtils.writeItemStack(buf, output);
}
buf.writeBoolean(task.getChild() != null);
if (task.getChild() != null) {
writeTask(buf, task.getChild());
}
}
@@ -70,13 +80,7 @@ public final class RefinedStorageSerializers {
List<ClientCraftingTask> tasks = new ArrayList<>();
for (int i = 0; i < size; ++i) {
String info = ByteBufUtils.readUTF8String(buf);
int outputs = buf.readInt();
for (int j = 0; j < outputs; ++j) {
tasks.add(new ClientCraftingTask(ByteBufUtils.readItemStack(buf), i, info));
}
readTask(buf, i, false, tasks);
}
Collections.reverse(tasks);
@@ -84,6 +88,20 @@ public final class RefinedStorageSerializers {
return tasks;
}
private void readTask(PacketBuffer buf, int i, boolean isChild, List<ClientCraftingTask> tasks) {
String status = ByteBufUtils.readUTF8String(buf);
int outputs = buf.readInt();
for (int j = 0; j < outputs; ++j) {
tasks.add(new ClientCraftingTask(ByteBufUtils.readItemStack(buf), i, status, isChild));
}
if (buf.readBoolean()) {
readTask(buf, i, true, tasks);
}
}
@Override
public DataParameter<List<ClientCraftingTask>> createKey(int id) {
return null;