From ae2dd5732a585de1223955e599777d1e990c230e Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 14 Jan 2020 13:32:19 -0500 Subject: [PATCH] Objectification --- .../mindustry/entities/type/TileEntity.java | 6 +-- core/src/mindustry/game/EventType.java | 4 +- core/src/mindustry/game/Schematics.java | 2 +- core/src/mindustry/game/Teams.java | 4 +- core/src/mindustry/input/InputHandler.java | 14 +---- core/src/mindustry/io/TypeIO.java | 51 ++++++++++++++++++- core/src/mindustry/net/Administration.java | 7 +-- core/src/mindustry/world/Block.java | 16 ++++-- 8 files changed, 77 insertions(+), 27 deletions(-) diff --git a/core/src/mindustry/entities/type/TileEntity.java b/core/src/mindustry/entities/type/TileEntity.java index cf5a2f4bdb..9db5eb9588 100644 --- a/core/src/mindustry/entities/type/TileEntity.java +++ b/core/src/mindustry/entities/type/TileEntity.java @@ -246,9 +246,9 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{ return proximity; } - /** Tile configuration. Defaults to 0. Used for block rebuilding. */ - public int config(){ - return 0; + /** Tile configuration. Defaults to null, which means 'no config'. Used for block rebuilding and schematics. */ + public Object config(){ + return null; } @Override diff --git a/core/src/mindustry/game/EventType.java b/core/src/mindustry/game/EventType.java index 365e8f2b7c..741ed132c3 100644 --- a/core/src/mindustry/game/EventType.java +++ b/core/src/mindustry/game/EventType.java @@ -186,9 +186,9 @@ public class EventType{ public static class TapConfigEvent{ public final Tile tile; public final Player player; - public final int value; + public final Object value; - public TapConfigEvent(Tile tile, Player player, int value){ + public TapConfigEvent(Tile tile, Player player, Object value){ this.tile = tile; this.player = player; this.value = value; diff --git a/core/src/mindustry/game/Schematics.java b/core/src/mindustry/game/Schematics.java index 7a96ed672b..ca64255332 100644 --- a/core/src/mindustry/game/Schematics.java +++ b/core/src/mindustry/game/Schematics.java @@ -345,7 +345,7 @@ public class Schematics implements Loadable{ if(tile != null && tile.entity != null && !counted.contains(tile.pos()) && !(tile.block() instanceof BuildBlock) && (tile.entity.block.isVisible() || (tile.entity.block instanceof CoreBlock && Core.settings.getBool("coreselect")))){ - int config = tile.entity.config(); + Object config = tile.entity.config(); if(tile.block().posConfig){ config = Pos.get(Pos.x(config) + offsetX, Pos.y(config) + offsetY); } diff --git a/core/src/mindustry/game/Teams.java b/core/src/mindustry/game/Teams.java index e4fa1f1ef3..2df98bd612 100644 --- a/core/src/mindustry/game/Teams.java +++ b/core/src/mindustry/game/Teams.java @@ -170,9 +170,9 @@ public class Teams{ * This does not include deconstructed blocks.*/ public static class BrokenBlock{ public final short x, y, rotation, block; - public final int config; + public final Object config; - public BrokenBlock(short x, short y, short rotation, short block, int config){ + public BrokenBlock(short x, short y, short rotation, short block, Object config){ this.x = x; this.y = y; this.rotation = rotation; diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index 8000fc4e19..d82358cfe1 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -151,19 +151,9 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ } } - @Remote(targets = Loc.both, called = Loc.server, forward = true) - public static void onTileTapped(Player 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(Player player, Tile tile, int value){ + public static void onTileConfig(Player player, Tile tile, @Nullable Object value){ if(tile == null) return; - if(net.server() && (!Units.canInteract(player, tile) || !netServer.admins.allowAction(player, ActionType.configure, tile, action -> action.config = value))) throw new ValidateException(player, "Player cannot configure a tile."); tile.block().configured(tile, player, value); @@ -588,7 +578,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ //call tapped event if(!consumed && tile.interactable(player.getTeam())){ - Call.onTileTapped(player, tile); + Call.onTileConfig(player, tile, null); } //consume tap event if necessary diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index 5095ce396a..24e26d1fc2 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -1,9 +1,10 @@ package mindustry.io; +import arc.struct.*; import mindustry.annotations.Annotations.ReadClass; import mindustry.annotations.Annotations.WriteClass; import arc.graphics.Color; -import mindustry.ctype.ContentType; +import mindustry.ctype.*; import mindustry.entities.Effects; import mindustry.entities.Effects.Effect; import mindustry.entities.type.Bullet; @@ -28,6 +29,54 @@ import static mindustry.Vars.*; @SuppressWarnings("unused") public class TypeIO{ + @WriteClass(Object.class) + public static void writeObject(ByteBuffer buffer, Object object){ + if(object == null){ + buffer.put((byte)0); + }else if(object instanceof Integer){ + buffer.put((byte)1); + buffer.putInt((Integer)object); + }else if(object instanceof Long){ + buffer.put((byte)2); + buffer.putLong((Long)object); + }else if(object instanceof Float){ + buffer.put((byte)3); + buffer.putFloat((Float)object); + }else if(object instanceof String){ + buffer.put((byte)4); + writeString(buffer, (String)object); + }else if(object instanceof Content){ + Content map = (Content)object; + buffer.put((byte)5); + buffer.put((byte)map.getContentType().ordinal()); + buffer.putShort(map.id); + }else if(object instanceof IntArray){ + buffer.put((byte)6); + IntArray arr = (IntArray)object; + buffer.putShort((short)arr.size); + for(int i = 0; i < arr.size; i++){ + buffer.putInt(arr.items[i]); + } + }else{ + throw new IllegalArgumentException("Unknown object type: " + object.getClass()); + } + } + + @ReadClass(Object.class) + public static Object readObject(ByteBuffer buffer){ + byte type = buffer.get(); + switch(type){ + case 0: return null; + case 1: return buffer.getInt(); + case 2: return buffer.getLong(); + case 3: return buffer.getFloat(); + case 4: return readString(buffer); + case 5: return content.getByID(ContentType.all[buffer.get()], buffer.getShort()); + case 6: short length = buffer.getShort(); IntArray arr = new IntArray(); for(int i = 0; i < length; i ++) arr.add(buffer.getInt()); return arr; + default: throw new IllegalArgumentException("Unknown object type: " + type); + } + } + @WriteClass(Player.class) public static void writePlayer(ByteBuffer buffer, Player player){ if(player == null){ diff --git a/core/src/mindustry/net/Administration.java b/core/src/mindustry/net/Administration.java index 8a5a438e14..1766341265 100644 --- a/core/src/mindustry/net/Administration.java +++ b/core/src/mindustry/net/Administration.java @@ -534,7 +534,7 @@ public class Administration{ public int rotation; /** valid for configure and rotation-type events only. */ - public int config; + public Object config; /** valid for item-type events only. */ public @Nullable Item item; @@ -550,7 +550,8 @@ public class Administration{ @Override public void reset(){ item = null; - itemAmount = config = 0; + itemAmount = 0; + config = null; player = null; type = null; tile = null; @@ -558,7 +559,7 @@ public class Administration{ } public enum ActionType{ - breakBlock, placeBlock, rotate, configure, tapTile, withdrawItem, depositItem + breakBlock, placeBlock, rotate, configure, withdrawItem, depositItem } } diff --git a/core/src/mindustry/world/Block.java b/core/src/mindustry/world/Block.java index a0294013ad..92528625c7 100644 --- a/core/src/mindustry/world/Block.java +++ b/core/src/mindustry/world/Block.java @@ -467,16 +467,26 @@ public class Block extends BlockStorage{ return cacheRegions[id]; } - /** Called when the block is tapped. */ + /** Called when the block is tapped. This is equivalent to being configured with null. */ public void tapped(Tile tile, Player player){ } - /** Called when arbitrary configuration is applied to a tile. */ - public void configured(Tile tile, @Nullable Player player, int value){ + /** Called when arbitrary int configuration is applied to a tile. */ + protected void configured_(Tile tile, @Nullable Player player, int value){ } + /** Called when arbitrary configuration is applied to a tile. + * The default behavior is to treat this as integer configuration. */ + public void configured(Tile tile, @Nullable Player player, @Nullable Object value){ + if(value instanceof Integer){ + configured_(tile, player, (int)value); + }else if(value == null){ + tapped(tile, player); + } + } + /** Returns whether or not a hand cursor should be shown over this block. */ public Cursor getCursor(Tile tile){ return configurable ? SystemCursor.hand : SystemCursor.arrow;