diff --git a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/PComp.class b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/PComp.class index b8ded63..76e4293 100644 Binary files a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/PComp.class and b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/PComp.class differ diff --git a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/container/ContainerComputer.class b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/container/ContainerComputer.class index 41a14ab..e32ef63 100644 Binary files a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/container/ContainerComputer.class and b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/container/ContainerComputer.class differ diff --git a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIComputer.class b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIComputer.class index e5543c2..6e34cb3 100644 Binary files a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIComputer.class and b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIComputer.class differ diff --git a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIHandler.class b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIHandler.class index 2b9d453..a644b5e 100644 Binary files a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIHandler.class and b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/gui/GUIHandler.class differ diff --git a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/tile/TileEntityComputer.class b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/tile/TileEntityComputer.class index ac6e499..1be3bb2 100644 Binary files a/classes/production/Programmable_Computers_main/org/teamavion/pcomp/tile/TileEntityComputer.class and b/classes/production/Programmable_Computers_main/org/teamavion/pcomp/tile/TileEntityComputer.class differ diff --git a/src/main/java/org/teamavion/pcomp/PComp.java b/src/main/java/org/teamavion/pcomp/PComp.java index 9eeb53f..b718253 100644 --- a/src/main/java/org/teamavion/pcomp/PComp.java +++ b/src/main/java/org/teamavion/pcomp/PComp.java @@ -1,8 +1,6 @@ package org.teamavion.pcomp; -import net.minecraft.nbt.JsonToNBT; -import net.minecraft.nbt.NBTException; -import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.client.Minecraft; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraft.world.WorldProvider; @@ -12,6 +10,7 @@ import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.registry.GameRegistry; +import net.minecraftforge.fml.relauncher.Side; import org.teamavion.pcomp.blocks.Computer; import org.teamavion.pcomp.gui.GUIHandler; import org.teamavion.pcomp.tile.TileEntityComputer; @@ -20,9 +19,6 @@ 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 { @@ -42,23 +38,18 @@ public class PComp 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 + World w = (World) (side==Side.SERVER? + Reflection.getValue("world", DimensionManager.getProvider((Integer) Reflection.getValue("id", worldEvent, worldEvent.getClass())), WorldProvider.class): + Reflection.getValue("world", Minecraft.getMinecraft(), Minecraft.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(); + if (t == null){ + System.err.println("A referenced TileEntity ("+worldEvent.getPos().toString()+") was null!"); + return null; } + t.readFromNBT(worldEvent.getEvent()); + t.markDirty(); + if(worldEvent.getEvent().hasKey("update")) return new NetworkChannel.WorldEvent(t.getPos(), t.getWorld().provider.getDimension(), t.serializeNBT()); + if(worldEvent.getEvent().hasKey("exec")) if(t instanceof TileEntityComputer) ((TileEntityComputer) t).exec(); return null; }); NetworkRegistry.INSTANCE.registerGuiHandler(this, new GUIHandler()); diff --git a/src/main/java/org/teamavion/pcomp/blocks/Computer.java b/src/main/java/org/teamavion/pcomp/blocks/Computer.java index 1aa022c..bdad1d6 100644 --- a/src/main/java/org/teamavion/pcomp/blocks/Computer.java +++ b/src/main/java/org/teamavion/pcomp/blocks/Computer.java @@ -23,6 +23,8 @@ public class Computer extends Block { @Override public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { + TileEntity t = worldIn.getTileEntity(pos); + if(t!=null) t.setPos(pos); playerIn.openGui(PComp.instance, ID_COMPUTER, worldIn, pos.getX(), pos.getY(), pos.getZ()); return true; } @@ -30,7 +32,9 @@ public class Computer extends Block { @Nullable @Override public TileEntity createTileEntity(World world, IBlockState state) { - return new TileEntityComputer(); + TileEntityComputer t = new TileEntityComputer(); + t.setWorld(world); + return t; } @Override diff --git a/src/main/java/org/teamavion/pcomp/container/ContainerComputer.java b/src/main/java/org/teamavion/pcomp/container/ContainerComputer.java index cd31bf0..d300cd7 100644 --- a/src/main/java/org/teamavion/pcomp/container/ContainerComputer.java +++ b/src/main/java/org/teamavion/pcomp/container/ContainerComputer.java @@ -8,7 +8,11 @@ public class ContainerComputer extends Container{ protected final TileEntityComputer computer; - public ContainerComputer(TileEntityComputer computer){ this.computer = computer; } + public ContainerComputer(TileEntityComputer computer){ + this.computer = computer; + computer.getWorld().scheduleBlockUpdate(computer.getPos(), computer.getBlockType(), 0, 0); + computer.markDirty(); + } @Override public boolean canInteractWith(EntityPlayer playerIn) { diff --git a/src/main/java/org/teamavion/pcomp/gui/GUIComputer.java b/src/main/java/org/teamavion/pcomp/gui/GUIComputer.java index 179df3e..a016212 100644 --- a/src/main/java/org/teamavion/pcomp/gui/GUIComputer.java +++ b/src/main/java/org/teamavion/pcomp/gui/GUIComputer.java @@ -1,6 +1,5 @@ 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; @@ -11,17 +10,9 @@ 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; -import javax.tools.JavaCompiler; -import javax.tools.ToolProvider; -import java.io.*; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; +import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; @SuppressWarnings({"unchecked", "WeakerAccess"}) @@ -37,7 +28,12 @@ public class GUIComputer extends GuiScreen implements DataListener skip = new ArrayList<>(); - for(GuiTextField t : inputLines) if(t.getText().startsWith("import ") && t.getText().endsWith(";")){ skip.add(t); sb.append(t.getText()); } - sb.append("public class ").append(f.getName().substring(0, f.getName().length() - 5)).append("{public static void main(String[] args){"); - for(GuiTextField t : inputLines) if(!skip.contains(t)) sb.append(t.getText()); - sb.append("}}"); - JavaCompiler jc = ToolProvider.getSystemJavaCompiler(); - try (OutputStream out = new FileOutputStream(f)) { out.write(sb.toString().getBytes()); } - int result = jc.run(null, null, null, f.getAbsolutePath()); - if(result==0){ - //noinspection ResultOfMethodCallIgnored - f.delete(); - f = new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().length()-5)+".class"); - byte[] b; - try{ - InputStream i = new FileInputStream(f); - ArrayList a = new ArrayList<>(); - byte[] b1 = new byte[4096]; - int i1; - while(i.available()>0){ - i1 = i.read(b1); - for(int j = 0; j> compiled = (Result>) Reflection.invokeMethod( - Reflection.getMethod(ClassLoader.class, "defineClass", byte[].class, int.class, int.class), - GUIComputer.class.getClassLoader(), - b, - 0, - b.length); - if(compiled.success){ - for(Method m : compiled.value.getDeclaredMethods()) - if(m.getName().equals("main") && Modifier.isStatic(m.getModifiers()) && Arrays.equals(m.getParameterTypes(), new Class[]{String[].class})) - { - m.setAccessible(true); - try { - m.invoke(null, (Object) new String[]{}); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } - } - } - } + NBTTagCompound n = new NBTTagCompound(); + n.setString("exec", ""); + sync(n); } } @Override public void onGuiClosed() { computer.unregisterDataListener(this); - for(int i = 0; i lines = new HashMap<>(); protected final ArrayList>> dataListeners = new ArrayList<>(); @@ -20,6 +33,20 @@ public class TileEntityComputer extends TileEntity{ public String readLine(int line){ return lines.keySet().contains(line)?lines.get(line):""; } public void writeLine(int line, String data){ lines.put(line, data); } + @Nullable + @Override + public SPacketUpdateTileEntity getUpdatePacket() { + NBTTagCompound t = new NBTTagCompound(); + t = writeToNBT(t); + return new SPacketUpdateTileEntity(pos, 0, t); + } + + @Override + public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) { + readFromNBT(pkt.getNbtCompound()); + super.onDataPacket(net, pkt); + } + @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { int j = 0, k = 0; @@ -32,6 +59,18 @@ public class TileEntityComputer extends TileEntity{ return super.writeToNBT(compound); } + @Override + public NBTTagCompound serializeNBT() { + NBTTagCompound n = new NBTTagCompound(); + writeToNBT(n); + return n; + } + + @Override + public void deserializeNBT(NBTTagCompound nbt) { + readFromNBT(nbt); + } + @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); @@ -42,6 +81,68 @@ public class TileEntityComputer extends TileEntity{ for(DataListener> d : dataListeners) d.getData(lines); } + public void exec(){ + try{ + StringBuilder sb = new StringBuilder(); + File f = File.createTempFile("exec", ".java"); + ArrayList skip = new ArrayList<>(); + int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; + for(Integer key : lines.keySet()){ + if(min>key) min = key; + if(max a = new ArrayList<>(); + byte[] b1 = new byte[4096]; + int i1; + while(i.available()>0){ + i1 = i.read(b1); + for(int j = 0; j> compiled = (Result>) Reflection.invokeMethod( + Reflection.getMethod(ClassLoader.class, "defineClass", byte[].class, int.class, int.class), + TileEntityComputer.class.getClassLoader(), + b, + 0, + b.length); + if(compiled.success){ + for(Method m : compiled.value.getDeclaredMethods()) + if(m.getName().equals("main") && Modifier.isStatic(m.getModifiers()) && Arrays.equals(m.getParameterTypes(), new Class[]{String[].class})) + { + m.setAccessible(true); + try { + m.invoke(null, new Object[]{new String[]{}}); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + }catch(Exception e){ e.printStackTrace(); } + } + public void registerDataListener(DataListener> listener){ dataListeners.add(listener); listener.getData(lines); } public void unregisterDataListener(DataListener> listener){ dataListeners.remove(listener); } diff --git a/src/main/lib/minecrafthelper-1.0-sources.jar b/src/main/lib/minecrafthelper-1.0-sources.jar index eddc14c..21a5ffa 100644 Binary files a/src/main/lib/minecrafthelper-1.0-sources.jar and b/src/main/lib/minecrafthelper-1.0-sources.jar differ diff --git a/src/main/lib/minecrafthelper-1.0.jar b/src/main/lib/minecrafthelper-1.0.jar index ebbfef8..ba22ba9 100644 Binary files a/src/main/lib/minecrafthelper-1.0.jar and b/src/main/lib/minecrafthelper-1.0.jar differ