Skip to content

Commit

Permalink
more multiplayer/multisession fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jun 23, 2016
1 parent a69cdf8 commit 54759dc
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 169 deletions.
175 changes: 169 additions & 6 deletions 19/src/main/java/mobi/omegacentauri/raspberryjammod/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -34,19 +35,24 @@
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

public class APIHandler {
// world.checkpoint.save/restore, player.setting, world.setting(nametags_visible,*),
Expand Down Expand Up @@ -115,16 +121,27 @@ public class APIHandler {
public static final String PASSWORD_DATA = "passwords.dat";
private String salt = null;
private boolean authenticationSetup;
private List<HitDescription> hits = new LinkedList<HitDescription>();
protected List<ChatDescription> chats = new LinkedList<ChatDescription>();
private boolean restrictToSword = true;
private boolean detectLeftClick = false;
private static final int MAX_CHATS = 512;
private static final int MAX_HITS = 512;

public APIHandler(MCEventHandler eventHandler, PrintWriter writer) throws IOException {
this(eventHandler, writer, true);
}

public void close() {
eventHandler.unregisterAPIHandler(this);
}

public APIHandler(MCEventHandler eventHandler, PrintWriter writer, Boolean needAuthentication) throws IOException {
this.eventHandler = eventHandler;
this.writer = writer;
this.havePlayer = false;
this.playerMP = null;
eventHandler.registerAPIHandler(this);

if (needAuthentication) {
File pass = new File(PASSWORD_DATA);
Expand Down Expand Up @@ -500,13 +517,13 @@ else if (cmd.equals(WORLDSPAWNPARTICLE)) {
spawnParticle(scan);
}
else if (cmd.equals(EVENTSCLEAR)) {
eventHandler.clearAll();
clearAllEvents();
}
else if (cmd.equals(EVENTSBLOCKHITS)) {
sendLine(eventHandler.getHitsAndClear());
sendLine(getHitsAndClear());
}
else if (cmd.equals(EVENTSCHATPOSTS)) {
sendLine(eventHandler.getChatsAndClear());
sendLine(getChatsAndClear());
}
else if (cmd.equals(WORLDSETTING)) {
String setting = scan.next();
Expand All @@ -520,10 +537,10 @@ else if (setting.equals("pause_drawing")) // across connections
}
else if (cmd.equals(EVENTSSETTING)) {
String setting = scan.next();
if (setting.equals("restrict_to_sword")) // across connections
eventHandler.setRestrictToSword(scan.nextInt() != 0);
if (setting.equals("restrict_to_sword")) // connection-specific
restrictToSword = (scan.nextInt() != 0);
else if (setting.equals("detect_left_click")) // across connections
eventHandler.setDetectLeftClick(scan.nextInt() != 0);
detectLeftClick = (scan.nextInt() != 0);
}
else if (cmd.startsWith("camera.")) {
cameraCommand(cmd.substring(7), scan);
Expand Down Expand Up @@ -990,4 +1007,150 @@ static void globalMessage(String message) {
}
}
}

public void click(PlayerInteractEvent event, boolean right) {
EntityPlayer player = event.getEntityPlayer();

System.out.println("Click "+right);

if (player == null || player.getEntityWorld().isRemote != RaspberryJamMod.clientOnlyAPI )
return;

if (right || detectLeftClick) {
if (! restrictToSword || holdingSword(player)) {
synchronized(hits) {
if (hits.size() >= MAX_HITS)
hits.remove(0);
hits.add(new HitDescription(eventHandler.getWorlds(),event));
}
}
}
if (eventHandler.stopChanges) {
event.setCanceled(true);
}
}
private boolean holdingSword(EntityPlayer player) {
ItemStack item = player.getHeldItemMainhand();
if (item != null) {
return item.getItem() instanceof ItemSword;
}
return false;
}

public void setRestrictToSword(boolean value) {
restrictToSword = value;
}

public String getHitsAndClear() {
String out = "";

synchronized(hits) {
int count = hits.size();
for (HitDescription e : hits) {
if (out.length() > 0)
out += "|";
out += e.getDescription();
}
hits.clear();
}

return out;
}

public String getChatsAndClear() {
StringBuilder out = new StringBuilder();

synchronized(chats) {
int count = hits.size();
for (ChatDescription c : chats) {
if (out.length() > 0)
out.append("|");
out.append(c.id);
out.append(",");
out.append(c.message.replace("&","&amp;").replace("|", "&#124;"));
}
chats.clear();
}

return out.toString();
}

public int eventCount() {
synchronized(hits) {
return hits.size();
}
}

public void clearHits() {
synchronized(hits) {
hits.clear();
}
}

public void clearChats() {
synchronized(chats) {
chats.clear();
}
}

public void clearAllEvents() {
hits.clear();
chats.clear();
}

public void addChatDescription(ChatDescription cd) {
synchronized(chats) {
if (chats.size() >= MAX_CHATS)
chats.remove(0);
chats.add(cd);
}
}

static class ChatDescription {
int id;
String message;
public ChatDescription(int entityId, String message) {
this.id = entityId;
this.message = message;
}
}


static class HitDescription {
private String description;

public HitDescription(World[] worlds, PlayerInteractEvent event) {
Vec3i pos = Location.encodeVec3i(worlds,
event.getEntityPlayer().getEntityWorld(),
event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
description = ""+pos.getX()+","+pos.getY()+","+pos.getZ()+","+
numericFace(event.getFace())+","+event.getEntity().getEntityId();
}

private int numericFace(EnumFacing face) {
switch(face) {
case DOWN:
return 0;
case UP:
return 1;
case NORTH:
return 2;
case SOUTH:
return 3;
case WEST:
return 4;
case EAST:
return 5;
default:
return 7;
}
}

public String getDescription() {
return description;
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ private void socketCommunicate(Socket connectionSocket) {
synchronized(socketList) {
socketList.add(connectionSocket);
}
APIHandler api = null;

try {
String clientSentence;

reader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
writer = new PrintWriter(connectionSocket.getOutputStream());

APIHandler api = controlServer ? new APIHandler(eventHandler, writer) :
api = controlServer ? new APIHandler(eventHandler, writer) :
new APIHandlerClientOnly(eventHandler, writer);

while(null != (clientSentence = reader.readLine())) {
Expand All @@ -148,6 +149,8 @@ private void socketCommunicate(Socket connectionSocket) {
System.out.println(""+e);
}
finally {
if (api != null)
api.close();
synchronized(socketList) {
socketList.remove(connectionSocket);
}
Expand Down
Loading

0 comments on commit 54759dc

Please sign in to comment.