Fixed some network crashes and block config problems

This commit is contained in:
Anuken
2018-01-10 17:22:19 -05:00
parent f09394c0ea
commit 1820abe14f
12 changed files with 94 additions and 37 deletions

View File

@@ -200,31 +200,37 @@ public class NetClient extends Module {
Net.handle(BlockUpdatePacket.class, packet -> {
Tile tile = Vars.world.tile(packet.position % Vars.world.width(), packet.position / Vars.world.width());
if(tile.entity != null){
if(tile != null && tile.entity != null){
tile.entity.health = packet.health;
}
});
Net.handle(BlockSyncPacket.class, packet -> {
if(!gotEntities) return;
DataInputStream stream = new DataInputStream(packet.stream);
try{
while(stream.available() > 0){
int pos = stream.readInt();
Gdx.app.postRunnable(() -> {
try {
while (stream.available() > 0) {
int pos = stream.readInt();
Tile tile = Vars.world.tile(pos % Vars.world.width(), pos / Vars.world.width());
//TODO what if there's no entity?
Tile tile = Vars.world.tile(pos % Vars.world.width(), pos / Vars.world.width());
byte times = stream.readByte();
byte times = stream.readByte();
for(int i = 0; i < times; i ++){
tile.entity.timer.getTimes()[i] = stream.readFloat();
for (int i = 0; i < times; i++) {
tile.entity.timer.getTimes()[i] = stream.readFloat();
}
tile.entity.read(stream);
}
tile.entity.read(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}catch (IOException e){
e.printStackTrace();
}
});
});
Net.handle(DisconnectPacket.class, packet -> {
@@ -247,17 +253,24 @@ public class NetClient extends Module {
});
Net.handle(WeaponSwitchPacket.class, packet -> {
Player player = Vars.control.playerGroup.getByID(packet.playerid);
Gdx.app.postRunnable(() -> {
Player player = Vars.control.playerGroup.getByID(packet.playerid);
if(player == null) return;
if(player == null) return;
player.weaponLeft = (Weapon)Upgrade.getByID(packet.left);
player.weaponRight = (Weapon)Upgrade.getByID(packet.right);
player.weaponLeft = (Weapon)Upgrade.getByID(packet.left);
player.weaponRight = (Weapon)Upgrade.getByID(packet.right);
});
});
Net.handle(BlockTapPacket.class, packet -> {
Tile tile = Vars.world.tile(packet.position);
tile.block().tapped(tile);
if(tile != null) tile.block().tapped(tile);
});
Net.handle(BlockConfigPacket.class, packet -> {
Tile tile = Vars.world.tile(packet.position);
if(tile != null) tile.block().configure(tile, packet.data);
});
}
@@ -272,6 +285,13 @@ public class NetClient extends Module {
}
}
public void handleBlockConfig(Tile tile, byte data){
BlockConfigPacket packet = new BlockConfigPacket();
packet.data = data;
packet.position = tile.packedPosition();
Net.send(packet, SendMode.tcp);
}
public void handleBlockTap(Tile tile){
BlockTapPacket packet = new BlockTapPacket();
packet.position = tile.packedPosition();

View File

@@ -168,6 +168,13 @@ public class NetServer extends Module{
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
});
Net.handleServer(BlockConfigPacket.class, packet -> {
Tile tile = Vars.world.tile(packet.position);
if(tile != null) tile.block().configure(tile, packet.data);
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
});
}
public void sendMessage(String message){

View File

@@ -186,7 +186,7 @@ public abstract class InputHandler extends InputAdapter{
placeBlockInternal(x, y, result, rotation, effects, sound);
if(Net.active()){
if(Net.active() && result != ProductionBlocks.core){
Vars.netClient.handlePlace(x, y, result, rotation);
}
}

View File

@@ -74,6 +74,7 @@ public class SaveIO{
}
}
/**Returns whether or not conversion was succesful.*/
public static boolean checkConvert(int slot){
try{
@@ -88,12 +89,12 @@ public class SaveIO{
target.read(stream);
stream.close();
saveToSlot(slot);
return true;
}
return false;
}catch (IOException e){
throw new RuntimeException(e);
return true;
}catch (Exception e){
return false;
}
}

View File

@@ -35,9 +35,15 @@ public class Saves {
}
public void convertSaves(){
Array<SaveSlot> invalid = new Array<>();
for(SaveSlot slot : saves){
SaveIO.checkConvert(slot.index);
if(!SaveIO.checkConvert(slot.index)){
invalid.add(slot);
}
}
saves.removeAll(invalid, true);
}
public SaveSlot getCurrent() {

View File

@@ -127,4 +127,9 @@ public class Packets {
public static class BlockTapPacket{
public int position;
}
public static class BlockConfigPacket{
public int position;
public byte data;
}
}

View File

@@ -37,6 +37,7 @@ public class Registrator {
UpgradePacket.class,
WeaponSwitchPacket.class,
BlockTapPacket.class,
BlockConfigPacket.class,
Class.class,
byte[].class,

View File

@@ -109,6 +109,11 @@ public class Block{
public void tapped(Tile tile){}
public void buildTable(Tile tile, Table table) {}
public void configure(Tile tile, byte data){}
public void setConfigure(Tile tile, byte data){
Vars.netClient.handleBlockConfig(tile, data);
}
public boolean isConfigurable(Tile tile){
return false;

View File

@@ -90,6 +90,14 @@ public class Sorter extends Junction{
return to;
}
@Override
public void configure(Tile tile, byte data) {
SorterEntity entity = tile.entity();
if(entity != null){
entity.sortItem = Item.getByID(data);
}
}
@Override
public boolean isConfigurable(Tile tile){
return true;
@@ -113,6 +121,7 @@ public class Sorter extends Junction{
final int f = i;
ImageButton button = cont.addImageButton("white", "toggle", 24, () -> {
entity.sortItem = items.get(f);
setConfigure(tile, (byte)f);
}).size(38, 42).padBottom(-5.1f).group(group).get();
button.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(Draw.region("icon-"+items.get(i).name)));
button.setChecked(entity.sortItem.id == f);

View File

@@ -46,6 +46,14 @@ public class Teleporter extends PowerBlock{
powerCapacity = 30f;
}
@Override
public void configure(Tile tile, byte data) {
TeleporterEntity entity = tile.entity();
if(entity != null){
entity.color = data;
}
}
@Override
public void getStats(Array<String> list){
super.getStats(list);
@@ -104,6 +112,7 @@ public class Teleporter extends PowerBlock{
ImageButton button = cont.addImageButton("white", "toggle", 24, () -> {
entity.color = (byte)f;
lastColor = (byte)f;
setConfigure(tile, (byte)f);
}).size(34, 38).padBottom(-5.1f).group(group).get();
button.getStyle().imageUpColor = colorArray[f];
button.setChecked(entity.color == f);