Objectification

This commit is contained in:
Anuken
2020-01-14 13:32:19 -05:00
parent 68be77fa1d
commit ae2dd5732a
8 changed files with 77 additions and 27 deletions
@@ -246,9 +246,9 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
return proximity; return proximity;
} }
/** Tile configuration. Defaults to 0. Used for block rebuilding. */ /** Tile configuration. Defaults to null, which means 'no config'. Used for block rebuilding and schematics. */
public int config(){ public Object config(){
return 0; return null;
} }
@Override @Override
+2 -2
View File
@@ -186,9 +186,9 @@ public class EventType{
public static class TapConfigEvent{ public static class TapConfigEvent{
public final Tile tile; public final Tile tile;
public final Player player; 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.tile = tile;
this.player = player; this.player = player;
this.value = value; this.value = value;
+1 -1
View File
@@ -345,7 +345,7 @@ public class Schematics implements Loadable{
if(tile != null && tile.entity != null && !counted.contains(tile.pos()) && !(tile.block() instanceof BuildBlock) 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")))){ && (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){ if(tile.block().posConfig){
config = Pos.get(Pos.x(config) + offsetX, Pos.y(config) + offsetY); config = Pos.get(Pos.x(config) + offsetX, Pos.y(config) + offsetY);
} }
+2 -2
View File
@@ -170,9 +170,9 @@ public class Teams{
* This does not include deconstructed blocks.*/ * This does not include deconstructed blocks.*/
public static class BrokenBlock{ public static class BrokenBlock{
public final short x, y, rotation, block; 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.x = x;
this.y = y; this.y = y;
this.rotation = rotation; this.rotation = rotation;
+2 -12
View File
@@ -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) @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(tile == null) return;
if(net.server() && (!Units.canInteract(player, tile) || 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."); !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); tile.block().configured(tile, player, value);
@@ -588,7 +578,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{
//call tapped event //call tapped event
if(!consumed && tile.interactable(player.getTeam())){ if(!consumed && tile.interactable(player.getTeam())){
Call.onTileTapped(player, tile); Call.onTileConfig(player, tile, null);
} }
//consume tap event if necessary //consume tap event if necessary
+50 -1
View File
@@ -1,9 +1,10 @@
package mindustry.io; package mindustry.io;
import arc.struct.*;
import mindustry.annotations.Annotations.ReadClass; import mindustry.annotations.Annotations.ReadClass;
import mindustry.annotations.Annotations.WriteClass; import mindustry.annotations.Annotations.WriteClass;
import arc.graphics.Color; import arc.graphics.Color;
import mindustry.ctype.ContentType; import mindustry.ctype.*;
import mindustry.entities.Effects; import mindustry.entities.Effects;
import mindustry.entities.Effects.Effect; import mindustry.entities.Effects.Effect;
import mindustry.entities.type.Bullet; import mindustry.entities.type.Bullet;
@@ -28,6 +29,54 @@ import static mindustry.Vars.*;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class TypeIO{ 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) @WriteClass(Player.class)
public static void writePlayer(ByteBuffer buffer, Player player){ public static void writePlayer(ByteBuffer buffer, Player player){
if(player == null){ if(player == null){
+4 -3
View File
@@ -534,7 +534,7 @@ public class Administration{
public int rotation; public int rotation;
/** valid for configure and rotation-type events only. */ /** valid for configure and rotation-type events only. */
public int config; public Object config;
/** valid for item-type events only. */ /** valid for item-type events only. */
public @Nullable Item item; public @Nullable Item item;
@@ -550,7 +550,8 @@ public class Administration{
@Override @Override
public void reset(){ public void reset(){
item = null; item = null;
itemAmount = config = 0; itemAmount = 0;
config = null;
player = null; player = null;
type = null; type = null;
tile = null; tile = null;
@@ -558,7 +559,7 @@ public class Administration{
} }
public enum ActionType{ public enum ActionType{
breakBlock, placeBlock, rotate, configure, tapTile, withdrawItem, depositItem breakBlock, placeBlock, rotate, configure, withdrawItem, depositItem
} }
} }
+13 -3
View File
@@ -467,16 +467,26 @@ public class Block extends BlockStorage{
return cacheRegions[id]; 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){ public void tapped(Tile tile, Player player){
} }
/** Called when arbitrary configuration is applied to a tile. */ /** Called when arbitrary int configuration is applied to a tile. */
public void configured(Tile tile, @Nullable Player player, int value){ 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. */ /** Returns whether or not a hand cursor should be shown over this block. */
public Cursor getCursor(Tile tile){ public Cursor getCursor(Tile tile){
return configurable ? SystemCursor.hand : SystemCursor.arrow; return configurable ? SystemCursor.hand : SystemCursor.arrow;