Net fixes

This commit is contained in:
Anuken 2019-04-08 18:33:22 -04:00
parent b40beb0d1d
commit 39fb2ae5ab
4 changed files with 31 additions and 49 deletions

View File

@ -115,26 +115,37 @@ public class NetClient implements ApplicationListener{
});
}
@Remote(called = Loc.server, targets = Loc.both, forward = true)
public static void sendMessage(Player player, String message){
if(message.length() > maxTextLength){
throw new ValidateException(player, "Player has sent a message above the text limit.");
}
Log.info("&y{0}: &lb{1}", (player == null || player.name == null ? "" : player.name), message);
//called on all clients
@Remote(called = Loc.server, targets = Loc.server)
public static void sendMessage(String message, String sender){
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, player == null ? null : colorizeName(player.id, player.name));
Vars.ui.chatfrag.addMessage(message, sender);
}
}
@Remote(called = Loc.server, variants = Variant.both, forward = true)
//equivalent to above method but there's no sender and no console log
@Remote(called = Loc.server, targets = Loc.server)
public static void sendMessage(String message){
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, null);
}
}
//called when a server recieves a chat message from a player
@Remote(called = Loc.server, targets = Loc.client)
public static void sendChatMessage(Player player, String message){
if(message.length() > maxTextLength){
throw new ValidateException(player, "Player has sent a message above the text limit.");
}
//server console logging
Log.info("&y{0}: &lb{1}", player.name, message);
//invoke event for all clients but also locally
//this is required so other clients get the correct name even if they don't know who's sending it yet
Call.sendMessage(message, colorizeName(player.id, player.name));
}
private static String colorizeName(int id, String name){
Player player = playerGroup.getByID(id);
if(name == null || player == null) return null;

View File

@ -16,7 +16,8 @@ import io.anuke.arc.util.io.CountableByteArrayOutputStream;
import io.anuke.mindustry.content.Blocks;
import io.anuke.mindustry.content.Mechs;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.*;
import io.anuke.mindustry.entities.Entities;
import io.anuke.mindustry.entities.EntityGroup;
import io.anuke.mindustry.entities.traits.BuilderTrait.BuildRequest;
import io.anuke.mindustry.entities.traits.Entity;
import io.anuke.mindustry.entities.traits.SyncTrait;
@ -32,19 +33,17 @@ import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.world.Tile;
import java.io.*;
import java.lang.StringBuilder;
import java.nio.ByteBuffer;
import java.util.zip.DeflaterOutputStream;
import static io.anuke.mindustry.Vars.*;
import java.lang.StringBuilder;
public class NetServer implements ApplicationListener{
public final static int maxSnapshotSize = 430;
private final static float serverSyncTime = 30, kickDuration = 30 * 1000;
private final static Vector2 vector = new Vector2();
private final static Rectangle viewport = new Rectangle();
private final static Array<Entity> returnArray = new Array<>();
/** If a player goes away of their server-side coordinates by this distance, they get teleported back. */
private final static float correctDist = 16f;
@ -460,38 +459,24 @@ public class NetServer implements ApplicationListener{
//check for syncable groups
for(EntityGroup<?> group : Entities.getAllGroups()){
if(group.isEmpty() || !(group.all().get(0) instanceof SyncTrait)) continue;
//clipping is done by representatives
SyncTrait represent = (SyncTrait)group.all().get(0);
//make sure mapping is enabled for this group
if(!group.mappingEnabled()){
throw new RuntimeException("Entity group '" + group.getType() + "' contains SyncTrait entities, yet mapping is not enabled. In order for syncing to work, you must enable mapping for this group.");
}
returnArray.clear();
if(represent.isClipped()){
EntityQuery.getNearby(group, viewport, entity -> {
if(((SyncTrait)entity).isSyncing() && viewport.overlaps(Tmp.r3.setSize(((SyncTrait)entity).clipSize(), ((SyncTrait)entity).clipSize()).setCenter(entity.getX(), entity.getY()))){
returnArray.add(entity);
}
});
}else{
for(Entity entity : group.all()){
if(((SyncTrait)entity).isSyncing()){
returnArray.add(entity);
}
}
}
syncStream.reset();
int sent = 0;
for(Entity entity : returnArray){
for(Entity entity : group.all()){
SyncTrait sync = (SyncTrait)entity;
if(!sync.isSyncing()) continue;
//write all entities now
dataStream.writeInt(entity.getID()); //write id
dataStream.writeByte(((SyncTrait)entity).getTypeID()); //write type ID
((SyncTrait)entity).write(dataStream); //write entity
dataStream.writeByte(sync.getTypeID()); //write type ID
sync.write(dataStream); //write entity
sent++;

View File

@ -1,8 +1,5 @@
package io.anuke.mindustry.entities.traits;
import io.anuke.arc.Core;
import io.anuke.arc.util.Tmp;
import io.anuke.mindustry.core.NetClient;
import io.anuke.mindustry.net.Interpolator;
import java.io.*;
@ -28,17 +25,6 @@ public interface SyncTrait extends Entity, TypeTrait{
throw new RuntimeException("This entity must have an interpolator to interpolate()!");
}
if(isClipped()){
//move off screen when no longer in bounds
Tmp.r1.setSize(Core.camera.width * NetClient.viewScale, Core.camera.height * NetClient.viewScale)
.setCenter(Core.camera.position.x, Core.camera.position.y);
if(!Tmp.r1.contains(getX(), getY()) && !Tmp.r1.contains(getInterpolator().last.x, getInterpolator().last.y)){
set(-99999f, -99999f);
return;
}
}
getInterpolator().update();
setX(getInterpolator().pos.x);

View File

@ -174,7 +174,7 @@ public class ChatFragment extends Table{
history.insert(1, message);
Call.sendMessage(player, message);
Call.sendChatMessage(message);
}
public void toggle(){