Error messages may occur.

Text sync almost complete
This commit is contained in:
Gabriel Tofvesson 2017-05-15 03:23:26 +02:00
parent 9009e98e84
commit b05a04b659
11 changed files with 110 additions and 1 deletions

View File

@ -1,5 +1,12 @@
package org.teamavion.pcomp;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@ -11,6 +18,10 @@ import org.teamavion.pcomp.tile.TileEntityComputer;
import org.teamavion.util.automation.BlockRegister;
import org.teamavion.util.automation.SetupHelper;
import org.teamavion.util.support.NetworkChannel;
import org.teamavion.util.support.Reflection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Mod(modid = PComp.MODID, version = PComp.VERSION)
public class PComp
@ -30,6 +41,26 @@ public class PComp
SetupHelper.setup(PComp.class);
SetupHelper.registerRenders(PComp.class);
channel = new NetworkChannel(MODID);
channel.registerWorldHandler((side, worldEvent) -> {
World w = (World) Reflection.getValue("world", DimensionManager.getProvider((Integer) Reflection.getValue("id", worldEvent, worldEvent.getClass())), WorldProvider.class); // Get World
TileEntity t = w.getTileEntity(worldEvent.getPos());
try {
if (t != null) {
Matcher m = Pattern.compile("(\\d+):(\\d+):(\\d+);(\\d+);(.+)").matcher(worldEvent.getData());
if(!m.matches()) throw new RuntimeException();
String s1;
NBTTagCompound n = JsonToNBT.getTagFromJson((s1=m.group(5)).substring(8, s1.length()-1)
.replace("&rbr;", "}")
.replace("&lbr;", "{")
.replace(""", "\"")
.replace("&", "&"));
t.readFromNBT(n);
}
} catch (NBTException e) {
e.printStackTrace();
}
return null;
});
NetworkRegistry.INSTANCE.registerGuiHandler(this, new GUIHandler());
GameRegistry.registerTileEntity(TileEntityComputer.class, "computer");
}

View File

@ -1,11 +1,16 @@
package org.teamavion.pcomp.gui;
import com.google.gson.JsonObject;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.nbt.NBTTagCompound;
import org.lwjgl.input.Keyboard;
import org.teamavion.pcomp.PComp;
import org.teamavion.pcomp.net.DataListener;
import org.teamavion.pcomp.tile.TileEntityComputer;
import org.teamavion.util.support.NetworkChannel;
import org.teamavion.util.support.Reflection;
import org.teamavion.util.support.Result;
@ -17,9 +22,10 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@SuppressWarnings({"unchecked", "WeakerAccess"})
public class GUIComputer extends GuiScreen{
public class GUIComputer extends GuiScreen implements DataListener<HashMap<Integer, String>>{
protected final int viewportMin = -90, viewportMax = 45, maxInputLines = 128, textFieldLen = 240, textColor = 0xFFFFFF;
protected final long keyPressTimeout = 0; // timeout period between keypresses to minimize spam
@ -49,6 +55,7 @@ public class GUIComputer extends GuiScreen{
button = new GuiButton(-1, this.width / 2 - textFieldLen / 2, this.height / 2 + viewportMax + 30, "Execute");
button.width = textFieldLen;
inputLines[selected].setFocused(true);
computer.registerDataListener(this);
}
@Override
@ -235,9 +242,27 @@ public class GUIComputer extends GuiScreen{
}
}
@Override
public void onGuiClosed() {
computer.unregisterDataListener(this);
for(int i = 0; i<inputLines.length; ++i) computer.writeLine(i, inputLines[i].getText());
NBTTagCompound n = new NBTTagCompound();
computer.writeToNBT(n);
JsonObject j = new JsonObject();
j.addProperty("tag", n.toString().replace("&", "&amp;").replace("\"", "&quot;").replace("{", "&lbr;").replace("}", "&rbr;"));
PComp.instance.channel.sendToServer(new NetworkChannel.WorldEvent(computer.getPos(), computer.getWorld().provider.getDimension(), j));
computer.markDirty();
super.onGuiClosed();
}
@Override
protected void mouseReleased(int mouseX, int mouseY, int state) {
super.mouseReleased(mouseX, mouseY, state);
button.mouseReleased(mouseX, mouseY);
}
@Override
public void getData(HashMap<Integer, String> data) {
for(int i = 0; i<inputLines.length; ++i) inputLines[i].setText(data.getOrDefault(i, ""));
}
}

View File

@ -1,14 +1,20 @@
package org.teamavion.pcomp.gui;
import com.google.gson.JsonObject;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;
import org.teamavion.pcomp.PComp;
import org.teamavion.pcomp.container.ContainerComputer;
import org.teamavion.pcomp.tile.TileEntityComputer;
import org.teamavion.util.support.NetworkChannel;
import javax.annotation.Nullable;
import static org.teamavion.pcomp.PComp.ID_COMPUTER;
public class GUIHandler implements IGuiHandler{
@ -19,6 +25,10 @@ public class GUIHandler implements IGuiHandler{
case ID_COMPUTER:
TileEntity t = world.getTileEntity(new BlockPos(x, y, z));
if(!(t instanceof TileEntityComputer)) return null; // Something went wrong. ID-clash maybe?
NBTTagCompound n = new NBTTagCompound();
t.writeToNBT(n);JsonObject j = new JsonObject();
j.addProperty("tag", n.toString().replace("&", "&amp;").replace("\"", "&quot;").replace("{", "&lbr;").replace("}", "&rbr;"));
PComp.instance.channel.sendTo(new NetworkChannel.WorldEvent(new BlockPos(x, y, z), world.provider.getDimension(), j), (EntityPlayerMP) player);
return new ContainerComputer((TileEntityComputer) t);
}
return null;

View File

@ -0,0 +1,5 @@
package org.teamavion.pcomp.net;
public interface DataListener<T> {
void getData(T data);
}

View File

@ -1,10 +1,48 @@
package org.teamavion.pcomp.tile;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import org.teamavion.pcomp.net.DataListener;
import java.util.ArrayList;
import java.util.HashMap;
public class TileEntityComputer extends TileEntity{
protected final HashMap<Integer, String> lines = new HashMap<>();
protected final ArrayList<DataListener<HashMap<Integer, String>>> dataListeners = new ArrayList<>();
public boolean isUsableByPlayer(EntityPlayer player) {
return world.getTileEntity(getPos()) == this && player.getDistanceSq(getPos().getX() + 0.5, getPos().getY() + 0.5,getPos().getZ() + 0.5) < 64;
}
public String readLine(int line){ return lines.keySet().contains(line)?lines.get(line):""; }
public void writeLine(int line, String data){ lines.put(line, data); }
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
int j = 0, k = 0;
String s;
for(Integer i : lines.keySet()) if(i>j) j = i;
for(Integer i : lines.keySet()) if(i<k) k = i;
for(Integer i : lines.keySet()) if((s=lines.get(i)).length()!=0) compound.setString("line_"+i, s);
compound.setInteger("min", k);
compound.setInteger("max", j);
return super.writeToNBT(compound);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
if(!compound.hasKey("max") || !compound.hasKey("min")) return; // Fail
lines.clear();
int min = compound.getInteger("min"), max = compound.getInteger("max");
for(int i = min; (i-1<max) || (i == Integer.MIN_VALUE && max==Integer.MAX_VALUE); ++i) if(compound.hasKey("line_"+i)) lines.put(i, compound.getString("line_"+i));
for(DataListener<HashMap<Integer, String>> d : dataListeners) d.getData(lines);
}
public void registerDataListener(DataListener<HashMap<Integer, String>> listener){ dataListeners.add(listener); listener.getData(lines); }
public void unregisterDataListener(DataListener<HashMap<Integer, String>> listener){ dataListeners.remove(listener); }
}

Binary file not shown.