aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/roomedit/panes/GeneralEditPane.java
blob: c23d7c0db75e9d5bc4d26478f22d31a49bc0088c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package kr.syeyoung.dungeonsguide.roomedit.panes;

import kr.syeyoung.dungeonsguide.dungeon.roomfinder.DungeonRoom;
import kr.syeyoung.dungeonsguide.dungeon.roomfinder.DungeonRoomInfoRegistry;
import kr.syeyoung.dungeonsguide.e;
import kr.syeyoung.dungeonsguide.roomedit.EditingContext;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import kr.syeyoung.dungeonsguide.gui.elements.*;
import kr.syeyoung.dungeonsguide.roomprocessor.ProcessorFactory;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;

import java.awt.*;
import java.io.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.UUID;
import java.util.zip.GZIPOutputStream;

public class GeneralEditPane extends MPanel {
    private DungeonRoom dungeonRoom;

    private MLabelAndElement uuid;
    private MLabelAndElement name;
    private MLabelAndElement secrets;

    private MLabelAndElement shape;
    private MLabelAndElement rotation;
    private MLabelAndElement shape2;

    private MButton save;
    private MButton end;
    private MButton schematic;

    private MLabelAndElement roomProcessor;

    public GeneralEditPane(final DungeonRoom dungeonRoom) {
        this.dungeonRoom = dungeonRoom;
        {
            MLabel la;
            uuid = new MLabelAndElement("Room UUID: ", la = new MLabel());
            la.setText(dungeonRoom.getDungeonRoomInfo().getUuid().toString());
            uuid.setBounds(new Rectangle(0,0,getBounds().width, 20));
            add(uuid);
        }
        {
            MTextField la = new MTextField() {
                @Override
                public void edit(String str) {
                    dungeonRoom.getDungeonRoomInfo().setName(str);
                }
            };
            name = new MLabelAndElement("Room Name: ", la);
            la.setText(dungeonRoom.getDungeonRoomInfo().getName());
            name.setBounds(new Rectangle(0,20,getBounds().width, 20));
            add(name);
        }
        {
            final MIntegerSelectionButton la = new MIntegerSelectionButton(dungeonRoom.getDungeonRoomInfo().getTotalSecrets());
            la.setOnUpdate(new Runnable() {
                @Override
                public void run() {
                    dungeonRoom.getDungeonRoomInfo().setTotalSecrets(la.getData());
                }
            });
            secrets = new MLabelAndElement("Room Secrets: ", la);
            secrets.setBounds(new Rectangle(0,40,getBounds().width, 20));
            add(secrets);
        }

        {
            MLabel la;
            shape = new MLabelAndElement("Room Shape: ", la = new MLabel());
            la.setText(dungeonRoom.getDungeonRoomInfo().getShape()+"");
            shape.setBounds(new Rectangle(0,60,getBounds().width, 20));
            add(shape);
        }

        {
            MLabel la;
            rotation = new MLabelAndElement("Found Room Rotation: ", la = new MLabel());
            la.setText(dungeonRoom.getRoomMatcher().getRotation()+"");
            rotation.setBounds(new Rectangle(0,80,getBounds().width, 20));
            add(rotation);
        }
        {
            MLabel la;
            shape2 = new MLabelAndElement("Found Room Shape: ", la = new MLabel());
            la.setText(dungeonRoom.getShape()+"");
            shape2.setBounds(new Rectangle(0,100,getBounds().width, 20));
            add(shape2);
        }
        {
            final MStringSelectionButton mStringSelectionButton = new MStringSelectionButton(new ArrayList<String>(ProcessorFactory.getProcessors()), dungeonRoom.getDungeonRoomInfo().getProcessorId());
            roomProcessor = new MLabelAndElement("Room Processor: ", mStringSelectionButton);
            roomProcessor.setBounds(new Rectangle(0,120,getBounds().width, 20));
            add(roomProcessor);

            mStringSelectionButton.setOnUpdate(new Runnable() {
                @Override
                public void run() {
                    dungeonRoom.getDungeonRoomInfo().setProcessorId(mStringSelectionButton.getSelected());
                    dungeonRoom.updateRoomProcessor();
                }
            });
        }
        {
            end = new MButton();
            end.setText("End Editing Session");
            end.setOnActionPerformed(new Runnable() {
                @Override
                public void run() {
                    EditingContext.endEditingSession();
                }
            });
            end.setBackgroundColor(Color.green);
            end.setBounds(new Rectangle(0,140,getBounds().width, 20));
            add(end);
        }
        {
            schematic = new MButton();
            schematic.setText("Save Schematic");
            schematic.setOnActionPerformed(new Runnable() {
                @Override
                public void run() {
                    try {
                        NBTTagCompound nbtTagCompound2 = createNBT();

                        File f=new File(e.getDungeonsGuide().getConfigDir(), "schematics/"+
                                dungeonRoom.getDungeonRoomInfo().getName()+"-"+dungeonRoom.getDungeonRoomInfo().getUuid().toString()+"-"+ UUID.randomUUID()+".schematic");

                        Method method = null;
                        try {
                            method = NBTTagCompound.class.getDeclaredMethod("write", DataOutput.class);
                            method.setAccessible(true);
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                            return;
                        }
                        FileOutputStream fos = new FileOutputStream(f);
                        DataOutputStream dataoutputstream = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(fos)));

                        try
                        {
                            dataoutputstream.writeByte(nbtTagCompound2.getId());

                            dataoutputstream.writeUTF("Schematic");
                            method.invoke(nbtTagCompound2, dataoutputstream);
                        }
                        finally
                        {
                            dataoutputstream.close();
                        }
                        Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§eDungeons Guide §7:: §fSaved to "+f.getName()));
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }

                }
            });
            schematic.setBackgroundColor(Color.orange);
            schematic.setBounds(new Rectangle(0,180,getBounds().width, 20));
            add(schematic);
        }
        {
            if (dungeonRoom.getDungeonRoomInfo().isRegistered()) return;
            save = new MButton();
            save.setText("Save RoomData");
            save.setOnActionPerformed(new Runnable() {
                @Override
                public void run() {
                    DungeonRoomInfoRegistry.register(dungeonRoom.getDungeonRoomInfo());
                    remove(save);
                }
            });
            save.setBackgroundColor(Color.green);
            save.setBounds(new Rectangle(0,10,getBounds().width, 20));
            add(save);
        }
    }

    @Override
    public void resize(int parentWidth, int parentHeight) {
        this.setBounds(new Rectangle(5,5,parentWidth-10,parentHeight-10));
    }

    @Override
    public void onBoundsUpdate() {
        if (save != null)
            save.setBounds(new Rectangle(0,160,getBounds().width, 20));
        end.setBounds(new Rectangle(1,140,getBounds().width-2, 20));
        if (schematic != null)
        schematic.setBounds(new Rectangle(0,180,getBounds().width, 20));
    }

    private NBTTagCompound createNBT() {
        NBTTagCompound compound = new NBTTagCompound();
        compound.setShort("Width", (short) (dungeonRoom.getMax().getX() - dungeonRoom.getMin().getX() + 1));
        compound.setShort("Height", (short) 255);
        compound.setShort("Length", (short) (dungeonRoom.getMax().getZ() - dungeonRoom.getMin().getZ() + 1));
        int size =compound.getShort("Width") * compound.getShort("Height") * compound.getShort("Length");

        byte[] blocks = new byte[size];
        byte[] meta = new byte[size];
        byte[] extra = new byte[size];
        byte[] extranibble = new byte[(int) Math.ceil(size / 2.0)];

        boolean extraEx = false;
        NBTTagList tileEntitiesList = new NBTTagList();
        for (int x = 0; x < compound.getShort("Width"); x++) {
            for (int y = 0; y <  compound.getShort("Height"); y++) {
                for (int z = 0; z < compound.getShort("Length"); z++) {
                    int index = x + (y * compound.getShort("Length") + z) * compound.getShort("Width");
                    BlockPos pos = dungeonRoom.getRelativeBlockPosAt(x,y - 70,z);
                    IBlockState blockState = dungeonRoom.getContext().getWorld().getBlockState(pos);
                    boolean acc = dungeonRoom.canAccessRelative(x,z);
                    int id = Block.getIdFromBlock(blockState.getBlock());
                    blocks[index] = acc ? (byte) id : 0;
                    meta[index] = acc ? (byte) blockState.getBlock().getMetaFromState(blockState) : 0;
                    if ((extra[index] = (byte) ((acc ? id : 0) >> 8)) > 0) {
                        extraEx = true;
                    }

                    if (blockState.getBlock().hasTileEntity(blockState)) {
                        TileEntity tileEntity = dungeonRoom.getContext().getWorld().getTileEntity(pos);
                        try {
                            final NBTTagCompound tileEntityCompound = new NBTTagCompound();
                            tileEntity.writeToNBT(tileEntityCompound);
                            tileEntitiesList.appendTag(tileEntityCompound);
                        } catch (final Exception e) {
                            final BlockPos tePos = tileEntity.getPos();

                            blocks[index] = (byte) 7;
                            meta[index] = 0;
                            extra[index] = 0;
                        }
                    }
                }
            }
        }
        for (int i = 0; i < extranibble.length; i++) {
            if (i * 2 + 1 < extra.length) {
                extranibble[i] = (byte) ((extra[i * 2 + 0] << 4) | extra[i * 2 + 1]);
            } else {
                extranibble[i] = (byte) (extra[i * 2 + 0] << 4);
            }
        }


        compound.setByteArray("Blocks", blocks);
        compound.setByteArray("Data", meta);
        compound.setString("Materials", "Alpha");
        if (extraEx) {
            compound.setByteArray("AddBlocks", extranibble);
        }
        compound.setTag("Entities", new NBTTagList());
        compound.setTag("TileEntities", tileEntitiesList);

        return compound;
    }
}