Browse Source

Save screen

master
Martmists 2 years ago
parent
commit
d941ec3262
  1. 2
      src/main/java/com/martmists/ynet/YNetMod.java
  2. 53
      src/main/java/com/martmists/ynet/blockentities/ControllerBlockEntity.java
  3. 11
      src/main/java/com/martmists/ynet/blocks/ControllerBlock.java
  4. 3
      src/main/java/com/martmists/ynet/event/impl/FluidTickCallback.java
  5. 39
      src/main/java/com/martmists/ynet/network/ConnectorConfiguration.java
  6. 7
      src/main/java/com/martmists/ynet/network/Network.java
  7. 12
      src/main/java/com/martmists/ynet/screens/ControllerScreen.java

2
src/main/java/com/martmists/ynet/YNetMod.java

@ -89,7 +89,7 @@ public class YNetMod implements ModInitializer { @@ -89,7 +89,7 @@ public class YNetMod implements ModInitializer {
}
static <T extends BaseProvider> ProviderTickCallback<T> register(String name, int color, Class<T> clazz, ProviderTickCallback<T> callback) {
PROVIDER_NAMES.put(clazz, name.replace(":", "."));
PROVIDER_NAMES.put(clazz, name);
PROVIDERS.put(clazz, callback);
COLOR_MAP.put(clazz, color);
return callback;

53
src/main/java/com/martmists/ynet/blockentities/ControllerBlockEntity.java

@ -4,12 +4,17 @@ import com.martmists.ynet.YNetMod; @@ -4,12 +4,17 @@ import com.martmists.ynet.YNetMod;
import com.martmists.ynet.api.BaseProvider;
import com.martmists.ynet.event.ProviderTickCallback;
import com.martmists.ynet.network.Channel;
import com.martmists.ynet.network.ConnectorConfiguration;
import com.martmists.ynet.network.Network;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.BlockPos;
import java.util.*;
import java.util.stream.Collectors;
public class ControllerBlockEntity extends BlockEntity implements Tickable {
public Network network;
@ -53,5 +58,51 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable { @@ -53,5 +58,51 @@ public class ControllerBlockEntity extends BlockEntity implements Tickable {
// TODO:
// - fromTag and toTag
// - GUI
@Override
public void fromTag(CompoundTag tag) {
super.fromTag(tag);
CompoundTag customData = tag.getCompound("controllerData");
for (String key : customData.getKeys()) {
int channelIndex = Integer.parseInt(key.substring(8));
channels[channelIndex] = new Channel();
CompoundTag cData = customData.getCompound(key);
String cType = customData.getString("type");
channels[channelIndex].providerType = YNetMod.PROVIDER_NAMES.entrySet().stream()
.filter(e -> e.getValue().equals(cType))
.map(Map.Entry::getKey)
.findFirst().orElse(null);
if (channels[channelIndex].providerType == null) {
channels[channelIndex] = null;
continue;
}
ListTag connectors = cData.getList("connectors", NbtType.COMPOUND);
channels[channelIndex].connectorSettings = connectors.stream().map(t -> {
ConnectorConfiguration config = new ConnectorConfiguration();
config.fromTag((CompoundTag) t);
return config;
}).collect(Collectors.toSet());
}
}
@Override
public CompoundTag toTag(CompoundTag tag) {
CompoundTag customData = new CompoundTag();
for (int i = 0; i < 9; i++) {
Channel c = channels[i];
if (c != null) {
CompoundTag cData = new CompoundTag();
ListTag connectors = new ListTag();
c.connectorSettings.forEach((s) -> s.toTag(connectors));
cData.put("connectors", connectors);
cData.putString("type", YNetMod.PROVIDER_NAMES.getOrDefault(c.providerType, "null"));
customData.put("channel_"+i, cData);
}
}
tag.put("controllerData", customData);
return super.toTag(tag);
}
}

11
src/main/java/com/martmists/ynet/blocks/ControllerBlock.java

@ -33,17 +33,6 @@ public class ControllerBlock extends BlockWithEntity { @@ -33,17 +33,6 @@ public class ControllerBlock extends BlockWithEntity {
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient) {
// Open screen
// for now, print all connected providers
Set<BlockPos> providers = ((ControllerBlockEntity)world.getBlockEntity(pos)).network.getProviders(world);
System.out.println("Start of list");
for (BlockPos p : providers) {
System.out.println("Provider found at " + p + ": " + world.getBlockState(p).getBlock());
}
System.out.println("End of list");
}
if (!world.isClient) {
ContainerProviderRegistry.INSTANCE.openContainer(new Identifier("ynet:controller"), player, (buffer) -> {
buffer.writeBlockPos(pos);

3
src/main/java/com/martmists/ynet/event/impl/FluidTickCallback.java

@ -87,7 +87,8 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> { @@ -87,7 +87,8 @@ public class FluidTickCallback implements ProviderTickCallback<FluidProvider> {
if (receiverConfig.filter != null) {
Entry fe = e;
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj == fe.fluid)){
// TODO
if (Arrays.stream(receiverConfig.filter).noneMatch((obj) -> obj.equals(fe.fluid))){
continue;
}
}

39
src/main/java/com/martmists/ynet/network/ConnectorConfiguration.java

@ -1,7 +1,16 @@ @@ -1,7 +1,16 @@
package com.martmists.ynet.network;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.util.math.BlockPos;
import java.util.Arrays;
import java.util.stream.Collectors;
public class ConnectorConfiguration {
public ConnectorConfiguration() {
state = State.DISABLED;
@ -12,12 +21,38 @@ public class ConnectorConfiguration { @@ -12,12 +21,38 @@ public class ConnectorConfiguration {
public BlockPos providerPos;
public int priority;
// For items/fluids/etc
public Object[] filter;
public Item[] filter;
public enum State {
DISABLED,
INPUT,
OUTPUT
}
public void toTag(ListTag tag) {
CompoundTag data = new CompoundTag();
ListTag l = new ListTag();
Arrays.stream(filter).forEach(i -> {
CompoundTag t = new CompoundTag();
new ItemStack(i).toTag(t);
l.add(t);
});
data.putInt("priority", priority);
data.putInt("state", state.ordinal());
data.put("filter", l);
data.putIntArray("pos", new int[] { providerPos.getX(), providerPos.getY(), providerPos.getZ() });
}
public void fromTag(CompoundTag tag) {
// Same here
priority = tag.getInt("priority");
int[] pos = tag.getIntArray("pos");
providerPos = new BlockPos(pos[0], pos[1], pos[2]);
state = State.values()[tag.getInt("state")];
filter = tag.getList("filter", NbtType.COMPOUND).stream()
.map(t -> ItemStack.fromTag((CompoundTag)t).getItem())
.toArray(Item[]::new);
}
}

7
src/main/java/com/martmists/ynet/network/Network.java

@ -138,8 +138,10 @@ public class Network { @@ -138,8 +138,10 @@ public class Network {
for (BlockPos p : Arrays.asList(c.up(), c.down(), c.north(), c.south(), c.east(), c.west())) {
Block b = world.getBlockState(p).getBlock();
Set<Class<? extends BaseProvider>> providerTypes = new HashSet<>();
findProviderTypes(b.getClass(), providerTypes);
if (providerTypes.contains(type) || (type == BaseProvider.class && b instanceof BaseProvider)) {
if (tMap.get(b.getClass()) == null) {
findProviderTypes(b.getClass(), providerTypes);
}
if (tMap.get(b.getClass()).contains(type) || (type == BaseProvider.class && b instanceof BaseProvider)) {
providers.add(p);
}
}
@ -147,7 +149,6 @@ public class Network { @@ -147,7 +149,6 @@ public class Network {
return providers;
}
// Ugly hack by Pyrofab
private static Map<Class<?>, Set<Class<? extends BaseProvider>>> tMap = new HashMap<>();
private static void findProviderTypes(Class<?> cls, Set<Class<? extends BaseProvider>> ret) {

12
src/main/java/com/martmists/ynet/screens/ControllerScreen.java

@ -20,6 +20,7 @@ import spinnery.widget.api.Size; @@ -20,6 +20,7 @@ import spinnery.widget.api.Size;
import spinnery.widget.api.WVerticalScrollable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -77,6 +78,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -77,6 +78,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
if (channelSettingsPanel.getZ() != 100){
return;
}
sourceBlockEntity.markDirty();
int currentIndex = TYPE_LOOP.indexOf((currentChannel != null) ? currentChannel.providerType : null);
Class<? extends BaseProvider> nextType;
try {
@ -88,6 +90,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -88,6 +90,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
currentChannel = new Channel();
}
currentChannel.providerType = nextType;
currentChannel.connectorSettings = new HashSet<>();
sourceBlockEntity.channels[channelNum] = currentChannel;
if (nextType == null) {
currentChannel = null;
@ -97,7 +100,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -97,7 +100,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
channelButtons[channelNum].overrideStyle("background.on", YNetMod.COLOR_MAP.get(nextType));
channelButtons[channelNum].overrideStyle("background.off", YNetMod.COLOR_MAP.get(nextType));
}
channelTypeButton.setLabel(YNetMod.PROVIDER_NAMES.getOrDefault(nextType, "Disabled"));
channelTypeButton.setLabel(YNetMod.PROVIDER_NAMES.getOrDefault(nextType, "Disabled").replace(":", "."));
});
WPanel connectorSettingsPanel = mainPanel.createChild(
@ -120,6 +123,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -120,6 +123,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
if (connectorSettingsPanel.getZ() != 100){
return;
}
sourceBlockEntity.markDirty();
ConnectorConfiguration.State nextState;
switch (currentConfig.state) {
case DISABLED:
@ -166,7 +170,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -166,7 +170,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
channelSettingsPanel.setLabel("Channel " + (f + 1));
if (currentChannel != null) {
channelTypeButton.setLabel(new TranslatableText(YNetMod.PROVIDER_NAMES.get(currentChannel.providerType)));
channelTypeButton.setLabel(new TranslatableText(YNetMod.PROVIDER_NAMES.get(currentChannel.providerType).replace(":", ".")));
}
});
@ -209,7 +213,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -209,7 +213,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
Channel channel = sourceBlockEntity.channels[j];
if (channel != null) {
ConnectorConfiguration config = channel.connectorSettings.stream().filter((s) -> s.providerPos == p).findFirst().orElse(null);
ConnectorConfiguration config = channel.connectorSettings.stream().filter((s) -> s.providerPos.equals(p)).findFirst().orElse(null);
if (config == null) {
config = new ConnectorConfiguration();
config.providerPos = p;
@ -230,7 +234,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> { @@ -230,7 +234,7 @@ public class ControllerScreen extends BaseContainerScreen<ControllerContainer> {
button.setOnMouseClicked((WButton widget, int mouseX, int mouseY, int mouseButton) -> {
if (sourceBlockEntity.channels[k] != null) {
currentConfig = sourceBlockEntity.channels[k].connectorSettings.stream().filter((s) -> s.providerPos == p).findFirst().orElse(null);
currentConfig = sourceBlockEntity.channels[k].connectorSettings.stream().filter((s) -> s.providerPos.equals(p)).findFirst().orElse(null);
if (currentConfig == null) {
currentConfig = new ConnectorConfiguration();
currentConfig.providerPos = p;

Loading…
Cancel
Save