From aeae286273bbe0a26786d8a1b0d0f40b837d71af Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 4 Mar 2020 08:38:36 -0500 Subject: [PATCH] Merge branch '6.0' of https://github.com/Anuken/Mindustry into object-config # Conflicts: # core/src/mindustry/entities/traits/BuilderTrait.java # core/src/mindustry/entities/type/TileEntity.java # core/src/mindustry/game/EventType.java # core/src/mindustry/game/Schematics.java # core/src/mindustry/input/InputHandler.java # core/src/mindustry/io/TypeIO.java # core/src/mindustry/world/Block.java # core/src/mindustry/world/blocks/distribution/Sorter.java --- core/src/mindustry/entities/def/TileComp.java | 12 ++++++++---- .../entities/units/BuildRequest.java | 4 ++-- core/src/mindustry/game/Schematic.java | 4 ++-- core/src/mindustry/game/Schematics.java | 19 +++++++++---------- core/src/mindustry/input/InputHandler.java | 19 +++++-------------- core/src/mindustry/input/MobileInput.java | 4 ++-- core/src/mindustry/io/SaveVersion.java | 4 ++-- core/src/mindustry/io/TypeIO.java | 4 ++-- core/src/mindustry/net/Administration.java | 2 +- core/src/mindustry/world/Block.java | 7 ++++--- .../world/blocks/distribution/ItemBridge.java | 8 ++------ .../world/blocks/distribution/MassDriver.java | 3 +-- .../world/blocks/distribution/Sorter.java | 9 +++++---- .../world/blocks/power/LightBlock.java | 2 +- .../world/blocks/power/PowerNode.java | 2 +- .../world/blocks/sandbox/ItemSource.java | 2 +- .../world/blocks/sandbox/LiquidSource.java | 8 +++++--- .../world/blocks/storage/Unloader.java | 2 +- .../world/blocks/units/CommandCenter.java | 2 +- 19 files changed, 55 insertions(+), 62 deletions(-) diff --git a/core/src/mindustry/entities/def/TileComp.java b/core/src/mindustry/entities/def/TileComp.java index 5807b9746c..58f7f15956 100644 --- a/core/src/mindustry/entities/def/TileComp.java +++ b/core/src/mindustry/entities/def/TileComp.java @@ -232,10 +232,14 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{ return proximity; } - /** Tile configuration. Defaults to 0. Used for block rebuilding. */ - @Override - public int config(){ - return 0; + /** Tile configuration. Defaults to null. Used for block rebuilding. */ + public Object config(){ + return null; + } + + /** Sets the config object and casts it. Does nothing by default. */ + public void setConfig(Object config){ + } @Override diff --git a/core/src/mindustry/entities/units/BuildRequest.java b/core/src/mindustry/entities/units/BuildRequest.java index 740da58163..64629a2143 100644 --- a/core/src/mindustry/entities/units/BuildRequest.java +++ b/core/src/mindustry/entities/units/BuildRequest.java @@ -17,7 +17,7 @@ public class BuildRequest{ /** Whether this request comes with a config int. If yes, any blocks placed with this request will not call playerPlaced.*/ public boolean hasConfig; /** Config int. Not used unless hasConfig is true.*/ - public int config; + public Object config; /** Original position, only used in schematics.*/ public int originalX, originalY, originalWidth, originalHeight; @@ -101,7 +101,7 @@ public class BuildRequest{ return y*tilesize + block.offset(); } - public BuildRequest configure(int config){ + public BuildRequest configure(Object config){ this.config = config; this.hasConfig = true; return this; diff --git a/core/src/mindustry/game/Schematic.java b/core/src/mindustry/game/Schematic.java index 602e579828..030b00cc78 100644 --- a/core/src/mindustry/game/Schematic.java +++ b/core/src/mindustry/game/Schematic.java @@ -116,10 +116,10 @@ public class Schematic implements Publishable, Comparable{ public static class Stile{ public @NonNull Block block; public short x, y; - public int config; + public Object config; public byte rotation; - public Stile(Block block, int x, int y, int config, byte rotation){ + public Stile(Block block, int x, int y, Object config, byte rotation){ this.block = block; this.x = (short)x; this.y = (short)y; diff --git a/core/src/mindustry/game/Schematics.java b/core/src/mindustry/game/Schematics.java index 79674e70a2..1cb3b44f13 100644 --- a/core/src/mindustry/game/Schematics.java +++ b/core/src/mindustry/game/Schematics.java @@ -10,6 +10,7 @@ import arc.graphics.g2d.*; import arc.graphics.gl.*; import arc.util.*; import arc.util.ArcAnnotate.*; +import arc.util.io.*; import arc.util.io.Streams.*; import arc.util.serialization.*; import mindustry.*; @@ -20,6 +21,7 @@ import mindustry.game.EventType.*; import mindustry.game.Schematic.*; import mindustry.input.*; import mindustry.input.Placement.*; +import mindustry.io.*; import mindustry.world.*; import mindustry.world.blocks.*; import mindustry.world.blocks.production.*; @@ -35,7 +37,7 @@ public class Schematics implements Loadable{ public static final String base64Header = "bXNjaAB"; private static final byte[] header = {'m', 's', 'c', 'h'}; - private static final byte version = 0; + private static final byte version = 1; private static final int padding = 2; private static final int maxPreviewsMobile = 32; @@ -259,8 +261,8 @@ public class Schematics implements Loadable{ tile.set(st.block, state.rules.defaultTeam); tile.rotation(st.rotation); - if(st.block.posConfig){ - tile.configureAny(Pos.get(tile.x - st.x + Pos.x(st.config), tile.y - st.y + Pos.y(st.config))); + if(st.config instanceof Point2){ + tile.configureAny(Pos.get(tile.x - st.x + ((Point2)st.config).x, tile.y - st.y + ((Point2)st.config).y)); }else{ tile.configureAny(st.config); } @@ -348,7 +350,7 @@ public class Schematics implements Loadable{ && (tile.entity.block().isVisible() || (tile.entity.block() instanceof CoreBlock && Core.settings.getBool("coreselect")))){ Object config = tile.entity.config(); if(config instanceof Point2){ - config = Pos.get(Pos.x(config) + offsetX, Pos.y(config) + offsetY); + config = Pos.get(((Point2)config).x + offsetX, ((Point2)config).y + offsetY); } tiles.add(new Stile(tile.block(), tile.x + offsetX, tile.y + offsetY, config, tile.rotation())); @@ -398,10 +400,7 @@ public class Schematics implements Loadable{ } } - int ver; - if((ver = input.read()) != version){ - throw new IOException("Unknown version: " + ver); - } + int ver = input.read(); try(DataInputStream stream = new DataInputStream(new InflaterInputStream(input))){ short width = stream.readShort(), height = stream.readShort(); @@ -424,7 +423,7 @@ public class Schematics implements Loadable{ for(int i = 0; i < total; i++){ Block block = blocks.get(stream.readByte()); int position = stream.readInt(); - int config = stream.readInt(); + Object config = ver == 0 ? stream.readInt() : TypeIO.readObject(Reads.get(stream)); byte rotation = stream.readByte(); if(block != Blocks.air){ tiles.add(new Stile(block, Pos.x(position), Pos.y(position), config, rotation)); @@ -468,7 +467,7 @@ public class Schematics implements Loadable{ for(Stile tile : schematic.tiles){ stream.writeByte(blocks.orderedItems().indexOf(tile.block)); stream.writeInt(Pos.get(tile.x, tile.y)); - stream.writeInt(tile.config); + TypeIO.writeObject(Writes.get(stream), tile.config); stream.writeByte(tile.rotation); } } diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index 0b1b1379f8..dfc0fd5cbf 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -159,15 +159,6 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ } } - @Remote(targets = Loc.both, called = Loc.server, forward = true) - public static void onTileTapped(Playerc player, Tile tile){ - if(tile == null || player == null) return; - if(net.server() && (!Units.canInteract(player, tile) || - !netServer.admins.allowAction(player, ActionType.tapTile, tile, action -> {}))) throw new ValidateException(player, "Player cannot tap a tile."); - tile.block().tapped(tile, player); - Core.app.post(() -> Events.fire(new TapEvent(tile, player))); - } - @Remote(targets = Loc.both, called = Loc.both, forward = true) public static void onTileConfig(Playerc player, Tile tile, @Nullable Object value){ if(tile == null) return; @@ -318,13 +309,13 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ if(req.config instanceof Point2){ int corigin = x ? req.originalWidth/2 : req.originalHeight/2; - int nvalue = -((x ? Pos.x(req.config) : Pos.y(req.config)) - corigin) + corigin; + int nvalue = -((x ? ((Point2)req.config).x : ((Point2)req.config).y) - corigin) + corigin; if(x){ req.originalX = -(req.originalX - corigin) + corigin; - req.config = Pos.get(nvalue, Pos.y(req.config)); + req.config = Pos.get(nvalue, ((Point2)req.config).y); }else{ req.originalY = -(req.originalY - corigin) + corigin; - req.config = Pos.get(Pos.x(req.config), nvalue); + req.config = Pos.get(((Point2)req.config).x, nvalue); } } @@ -459,8 +450,8 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ for(BuildRequest req : requests){ if(req.block != null && validPlace(req.x, req.y, req.block, req.rotation)){ BuildRequest copy = req.copy(); - if(copy.hasConfig && copy.block.posConfig){ - copy.config = Pos.get(Pos.x(copy.config) + copy.x - copy.originalX, Pos.y(copy.config) + copy.y - copy.originalY); + if(copy.hasConfig && copy.config instanceof Point2){ + copy.config = Pos.get(((Point2)copy.config).x + copy.x - copy.originalX, ((Point2)copy.config).x + copy.y - copy.originalY); } player.builder().addBuild(copy); } diff --git a/core/src/mindustry/input/MobileInput.java b/core/src/mindustry/input/MobileInput.java index 81a6cccd9c..9ae45f444f 100644 --- a/core/src/mindustry/input/MobileInput.java +++ b/core/src/mindustry/input/MobileInput.java @@ -222,8 +222,8 @@ public class MobileInput extends InputHandler implements GestureListener{ BuildRequest other = getRequest(request.x, request.y, request.block.size, null); BuildRequest copy = request.copy(); - if(copy.hasConfig && copy.block.posConfig){ - copy.config = Pos.get(Pos.x(copy.config) + copy.x - copy.originalX, Pos.y(copy.config) + copy.y - copy.originalY); + if(copy.hasConfig && copy.config instanceof Point2){ + copy.config = Pos.get(((Point2)copy.config).x + copy.x - copy.originalX, ((Point2)copy.config).y + copy.y - copy.originalY); } if(other == null){ diff --git a/core/src/mindustry/io/SaveVersion.java b/core/src/mindustry/io/SaveVersion.java index 3e9075984c..0e18f358f3 100644 --- a/core/src/mindustry/io/SaveVersion.java +++ b/core/src/mindustry/io/SaveVersion.java @@ -221,7 +221,7 @@ public abstract class SaveVersion extends SaveFileReader{ stream.writeShort(block.y); stream.writeShort(block.rotation); stream.writeShort(block.block); - stream.writeInt(block.config); + TypeIO.writeObject(Writes.get(stream), block.config); } } @@ -243,7 +243,7 @@ public abstract class SaveVersion extends SaveFileReader{ TeamData data = team.data(); int blocks = stream.readInt(); for(int j = 0; j < blocks; j++){ - data.brokenBlocks.addLast(new BrokenBlock(stream.readShort(), stream.readShort(), stream.readShort(), content.block(stream.readShort()).id, stream.readInt())); + data.brokenBlocks.addLast(new BrokenBlock(stream.readShort(), stream.readShort(), stream.readShort(), content.block(stream.readShort()).id, TypeIO.readObject(Reads.get(stream)))); } } diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index efa88e229d..23ae0710af 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -110,7 +110,7 @@ public class TypeIO{ write.s(request.block.id); write.b((byte)request.rotation); write.b(request.hasConfig ? (byte)1 : 0); - write.i(request.config); + writeObject(write, request.config); } } } @@ -133,7 +133,7 @@ public class TypeIO{ short block = read.s(); byte rotation = read.b(); boolean hasConfig = read.b() == 1; - int config = read.i(); + Object config = readObject(read); currentRequest = new BuildRequest(Pos.x(position), Pos.y(position), rotation, content.block(block)); if(hasConfig){ currentRequest.configure(config); diff --git a/core/src/mindustry/net/Administration.java b/core/src/mindustry/net/Administration.java index 3f348a4889..a068dc9472 100644 --- a/core/src/mindustry/net/Administration.java +++ b/core/src/mindustry/net/Administration.java @@ -574,7 +574,7 @@ public class Administration{ } public enum ActionType{ - breakBlock, placeBlock, rotate, configure, withdrawItem, depositItem + breakBlock, placeBlock, rotate, configure, tapTile, withdrawItem, depositItem } } diff --git a/core/src/mindustry/world/Block.java b/core/src/mindustry/world/Block.java index 6c3b4548ac..20dea8483a 100644 --- a/core/src/mindustry/world/Block.java +++ b/core/src/mindustry/world/Block.java @@ -477,9 +477,10 @@ public class Block extends BlockStorage{ /** Called when arbitrary configuration is applied to a tile. */ public void configured(Tile tile, @Nullable Playerc player, @Nullable Object value){ if(value == null){ - tapped(tile, player); + //TODO + //tapped(tile, player); }else if(configurations.containsKey(value.getClass())){ - configurations.get(value.getClass()).configured(tile, player, value); + configurations.get(value.getClass()).configured(tile, value); } } @@ -929,7 +930,7 @@ public class Block extends BlockStorage{ } public interface ConfigHandler{ - void configured(Tile tile, Player player, T value); + void configured(Tile tile, T value); } } diff --git a/core/src/mindustry/world/blocks/distribution/ItemBridge.java b/core/src/mindustry/world/blocks/distribution/ItemBridge.java index 9fbe9a4779..afd1ca51b6 100644 --- a/core/src/mindustry/world/blocks/distribution/ItemBridge.java +++ b/core/src/mindustry/world/blocks/distribution/ItemBridge.java @@ -37,17 +37,13 @@ public class ItemBridge extends Block{ layer = Layer.power; expanded = true; itemCapacity = 10; - posConfig = true; configurable = true; hasItems = true; unloadable = false; group = BlockGroup.transportation; entityType = ItemBridgeEntity::new; - } - @Override - public void configured(Tile tile, Playerc player, int value){ - tile.ent().link = value; + config(Integer.class, (tile, i) -> tile.ent().link = i); } @Override @@ -371,7 +367,7 @@ public class ItemBridge extends Block{ public float cycleSpeed = 1f; @Override - public int config(){ + public Object config(){ return link; } diff --git a/core/src/mindustry/world/blocks/distribution/MassDriver.java b/core/src/mindustry/world/blocks/distribution/MassDriver.java index 1da475939a..d9571be083 100644 --- a/core/src/mindustry/world/blocks/distribution/MassDriver.java +++ b/core/src/mindustry/world/blocks/distribution/MassDriver.java @@ -36,7 +36,6 @@ public class MassDriver extends Block{ super(name); update = true; solid = true; - posConfig = true; configurable = true; hasItems = true; layer = Layer.turret; @@ -46,7 +45,7 @@ public class MassDriver extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ tile.ent().link = value; } diff --git a/core/src/mindustry/world/blocks/distribution/Sorter.java b/core/src/mindustry/world/blocks/distribution/Sorter.java index f640b4078d..be5347829f 100644 --- a/core/src/mindustry/world/blocks/distribution/Sorter.java +++ b/core/src/mindustry/world/blocks/distribution/Sorter.java @@ -30,7 +30,8 @@ public class Sorter extends Block{ configurable = true; unloadable = false; entityType = SorterEntity::new; - config(Item.class, (tile, player, item) -> tile.ent().sortItem = item); + + config(Item.class, (tile, item) -> tile.ent().sortItem = item); } @Override @@ -46,8 +47,8 @@ public class Sorter extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ - tile.ent().sortItem = content.item(value); + public void configured(Tile tile, Playerc player, Object value){ + tile.ent().sortItem = (Item)value; if(!headless){ renderer.minimap.update(tile); } @@ -148,7 +149,7 @@ public class Sorter extends Block{ @Nullable Item sortItem; @Override - public Item config(){ + public Object config(){ return sortItem; } diff --git a/core/src/mindustry/world/blocks/power/LightBlock.java b/core/src/mindustry/world/blocks/power/LightBlock.java index c8861c4a32..20bb4be208 100644 --- a/core/src/mindustry/world/blocks/power/LightBlock.java +++ b/core/src/mindustry/world/blocks/power/LightBlock.java @@ -62,7 +62,7 @@ public class LightBlock extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ tile.ent().color = value; } diff --git a/core/src/mindustry/world/blocks/power/PowerNode.java b/core/src/mindustry/world/blocks/power/PowerNode.java index a1c277a150..5ff957b1c3 100644 --- a/core/src/mindustry/world/blocks/power/PowerNode.java +++ b/core/src/mindustry/world/blocks/power/PowerNode.java @@ -38,7 +38,7 @@ public class PowerNode extends PowerBlock{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ Tilec entity = tile.entity; Tile other = world.tile(value); boolean contains = entity.power().links.contains(value), valid = other != null && other.entity != null && other.entity.power() != null; diff --git a/core/src/mindustry/world/blocks/sandbox/ItemSource.java b/core/src/mindustry/world/blocks/sandbox/ItemSource.java index f68653e2df..994fe0e0d5 100644 --- a/core/src/mindustry/world/blocks/sandbox/ItemSource.java +++ b/core/src/mindustry/world/blocks/sandbox/ItemSource.java @@ -30,7 +30,7 @@ public class ItemSource extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ tile.ent().outputItem = content.item(value); } diff --git a/core/src/mindustry/world/blocks/sandbox/LiquidSource.java b/core/src/mindustry/world/blocks/sandbox/LiquidSource.java index 31740856ea..4c4644814b 100644 --- a/core/src/mindustry/world/blocks/sandbox/LiquidSource.java +++ b/core/src/mindustry/world/blocks/sandbox/LiquidSource.java @@ -28,6 +28,8 @@ public class LiquidSource extends Block{ configurable = true; outputsLiquid = true; entityType = LiquidSourceEntity::new; + + config(); } @Override @@ -85,7 +87,7 @@ public class LiquidSource extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ tile.ent().source = value == -1 ? null : content.liquid(value); } @@ -93,8 +95,8 @@ public class LiquidSource extends Block{ public @Nullable Liquid source = null; @Override - public int config(){ - return source == null ? -1 : source.id; + public Object config(){ + return source; } @Override diff --git a/core/src/mindustry/world/blocks/storage/Unloader.java b/core/src/mindustry/world/blocks/storage/Unloader.java index 15d503079b..f09fb6868b 100644 --- a/core/src/mindustry/world/blocks/storage/Unloader.java +++ b/core/src/mindustry/world/blocks/storage/Unloader.java @@ -55,7 +55,7 @@ public class Unloader extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ tile.entity.items().clear(); tile.ent().sortItem = content.item(value); } diff --git a/core/src/mindustry/world/blocks/units/CommandCenter.java b/core/src/mindustry/world/blocks/units/CommandCenter.java index b4f62c5942..23e27bb48a 100644 --- a/core/src/mindustry/world/blocks/units/CommandCenter.java +++ b/core/src/mindustry/world/blocks/units/CommandCenter.java @@ -103,7 +103,7 @@ public class CommandCenter extends Block{ } @Override - public void configured(Tile tile, Playerc player, int value){ + public void configured(Tile tile, Playerc player, Object value){ UnitCommand command = UnitCommand.all[value]; ((CommandCenter)tile.block()).effect.at(tile);