Browse Source

fixed bug when saving plan

lookup-tables
Stephan Richter 5 years ago
parent
commit
44abb92ed5
  1. 29
      src/main/java/de/srsoftware/web4rail/Plan.java
  2. 9
      src/main/java/de/srsoftware/web4rail/tiles/Shadow.java
  3. 4
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

29
src/main/java/de/srsoftware/web4rail/Plan.java

@ -70,12 +70,12 @@ public class Plan {
int x = Integer.parseInt(xs); int x = Integer.parseInt(xs);
int y = Integer.parseInt(ys); int y = Integer.parseInt(ys);
if (clazz == null) throw new NullPointerException(TILE+" must not be null!"); if (clazz == null) throw new NullPointerException(TILE+" must not be null!");
Class<Tile> tc = Tile.class; Class<Tile> tc = Tile.class;
clazz = tc.getName().replace(".Tile", "."+clazz); clazz = tc.getName().replace(".Tile", "."+clazz);
Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance(); Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
set(x, y, tile); set(x, y, tile);
for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow()); for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow(tile));
for (int i=1; i<tile.height(); i++) set(x,y+1,new Shadow()); for (int i=1; i<tile.height(); i++) set(x,y+1,new Shadow(tile));
return tile; return tile;
} }
@ -105,7 +105,11 @@ public class Plan {
while (br.ready()) { while (br.ready()) {
String line = br.readLine().trim(); String line = br.readLine().trim();
String[] parts = line.split(":"); String[] parts = line.split(":");
result.addTile(parts[2].trim(), parts[0].trim(), parts[1].trim()); try {
result.addTile(parts[2].trim(), parts[0].trim(), parts[1].trim());
} catch (Exception e) {
LOG.warn("Was not able to load \"{}\":",line,e);
}
} }
br.close(); br.close();
return result; return result;
@ -188,7 +192,7 @@ public class Plan {
case ACTION_MOVE: case ACTION_MOVE:
return moveTile(params.get(DIRECTION),params.get(X),params.get(Y)); return moveTile(params.get(DIRECTION),params.get(X),params.get(Y));
case ACTION_PROPS: case ACTION_PROPS:
return Tile.propMenu(); return propMenu(params.get(X),params.get(Y));
case ACTION_SAVE: case ACTION_SAVE:
return saveTo(params.get(NAME)); return saveTo(params.get(NAME));
default: default:
@ -200,6 +204,17 @@ public class Plan {
} }
} }
private Tag propMenu(String x, String y) {
return propMenu(Integer.parseInt(x),Integer.parseInt(y));
}
private Tag propMenu(int x, int y) {
Tile tile = get(x, y);
if (tile == null) return null;
if (tile instanceof Shadow) tile = ((Shadow)tile).overlay();
return tile.propMenu();
}
private String saveTo(String name) throws IOException { private String saveTo(String name) throws IOException {
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!"); if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");
File file = new File(name+".plan"); File file = new File(name+".plan");
@ -209,7 +224,7 @@ public class Plan {
for (Entry<Integer, Tile> row : column.getValue().entrySet()) { for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
int y = row.getKey(); int y = row.getKey();
Tile tile = row.getValue().position(x, y); Tile tile = row.getValue().position(x, y);
if (tile != null) br.append(x+":"+y+":"+tile.getClass().getSimpleName()+"\n"); if (tile != null && !(tile instanceof Shadow)) br.append(x+":"+y+":"+tile.getClass().getSimpleName()+"\n");
} }
} }
br.close(); br.close();

9
src/main/java/de/srsoftware/web4rail/tiles/Shadow.java

@ -2,4 +2,13 @@ package de.srsoftware.web4rail.tiles;
public class Shadow extends Tile{ public class Shadow extends Tile{
private Tile overlay;
public Shadow(Tile overlay) {
this.overlay = overlay;
}
public Tile overlay() {
return overlay;
}
} }

4
src/main/java/de/srsoftware/web4rail/tiles/Tile.java

@ -35,8 +35,8 @@ public abstract class Tile {
return this; return this;
} }
public static Tag propMenu() { public Tag propMenu() {
return new Window("tile-properties",t("Properties")).content(t("This tile has no properties")); return new Window("tile-properties",t("Properties")).content(t("This tile ({}) has no properties",getClass().getSimpleName()));
} }
public Tag tag() throws IOException { public Tag tag() throws IOException {

Loading…
Cancel
Save