Add config option for blocking and fix crash due to synchronization

This commit is contained in:
Myles 2016-03-09 12:08:21 +00:00
parent a743895c3f
commit b0050d4a8d
4 changed files with 35 additions and 23 deletions

View File

@ -116,23 +116,27 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI {
if (connection != null) { if (connection != null) {
for (Field field : connection.getClass().getDeclaredFields()) { for (Field field : connection.getClass().getDeclaredFields()) {
field.setAccessible(true); field.setAccessible(true);
Object value = field.get(connection); final Object value = field.get(connection);
if (value instanceof List) { if (value instanceof List) {
// Inject the list // Inject the list
field.set(connection, new ListWrapper((List) value) { field.set(connection, new ListWrapper((List) value) {
@Override @Override
public void handleAdd(Object o) { public void handleAdd(Object o) {
if (o instanceof ChannelFuture) { synchronized (value) {
inject((ChannelFuture) o); if (o instanceof ChannelFuture) {
inject((ChannelFuture) o);
}
} }
} }
}); });
// Iterate through current list // Iterate through current list
for (Object o : (List) value) { synchronized (value) {
if (o instanceof ChannelFuture) { for (Object o : (List) value) {
inject((ChannelFuture) o); if (o instanceof ChannelFuture) {
} else { inject((ChannelFuture) o);
break; // not the right list. } else {
break; // not the right list.
}
} }
} }
} }
@ -221,6 +225,10 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI {
return getConfig().getBoolean("suppress-metadata-errors", false); return getConfig().getBoolean("suppress-metadata-errors", false);
} }
public boolean isShieldBlocking() {
return getConfig().getBoolean("shield-blocking", true);
}
public boolean isAutoTeam() { public boolean isAutoTeam() {
// Collision has to be enabled first // Collision has to be enabled first
if (!isPreventCollision()) return false; if (!isPreventCollision()) return false;

View File

@ -16,6 +16,7 @@ import us.myles.ViaVersion.util.PacketUtil;
import java.io.IOException; import java.io.IOException;
public class IncomingTransformer { public class IncomingTransformer {
private final ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance();
private final ConnectionInfo info; private final ConnectionInfo info;
private boolean startedBlocking = false; private boolean startedBlocking = false;
@ -34,7 +35,7 @@ public class IncomingTransformer {
if (packet.getPacketID() != -1) { if (packet.getPacketID() != -1) {
packetID = packet.getPacketID(); packetID = packet.getPacketID();
} }
if (ViaVersion.getInstance().isDebug()) { if (plugin.isDebug()) {
if (packet != PacketType.PLAY_PLAYER_POSITION_LOOK_REQUEST && packet != PacketType.PLAY_KEEP_ALIVE_REQUEST && packet != PacketType.PLAY_PLAYER_POSITION_REQUEST && packet != PacketType.PLAY_PLAYER_LOOK_REQUEST) { if (packet != PacketType.PLAY_PLAYER_POSITION_LOOK_REQUEST && packet != PacketType.PLAY_KEEP_ALIVE_REQUEST && packet != PacketType.PLAY_PLAYER_POSITION_REQUEST && packet != PacketType.PLAY_PLAYER_LOOK_REQUEST) {
System.out.println("Direction " + packet.getDirection().name() + " Packet Type: " + packet + " New ID: " + packetID + " Original: " + original + " Size: " + input.readableBytes()); System.out.println("Direction " + packet.getDirection().name() + " Packet Type: " + packet + " New ID: " + packetID + " Original: " + original + " Size: " + input.readableBytes());
} }
@ -267,7 +268,7 @@ public class IncomingTransformer {
output.writeByte(face); output.writeByte(face);
PacketUtil.readVarInt(input); PacketUtil.readVarInt(input);
ItemStack inHand = ViaVersionPlugin.getHandItem(info); ItemStack inHand = plugin.getHandItem(info);
try { try {
ItemSlotRewriter.ItemStack item = ItemSlotRewriter.ItemStack.fromBukkit(inHand); ItemSlotRewriter.ItemStack item = ItemSlotRewriter.ItemStack.fromBukkit(inHand);
ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.fixIdsFrom1_9To1_8(item);
@ -291,8 +292,8 @@ public class IncomingTransformer {
output.writeLong(-1L); output.writeLong(-1L);
output.writeByte(255); output.writeByte(255);
// write item in hand // write item in hand
ItemStack inHand = ViaVersionPlugin.getHandItem(info); ItemStack inHand = plugin.getHandItem(info);
if (inHand != null) { if (inHand != null && plugin.isShieldBlocking()) {
if (inHand.getType().name().endsWith("SWORD")) { if (inHand.getType().name().endsWith("SWORD")) {
// blocking? // blocking?
if (hand == 0) { if (hand == 0) {

View File

@ -830,17 +830,18 @@ public class OutgoingTransformer {
if (entry.getOldID() == 0) { if (entry.getOldID() == 0) {
// Byte // Byte
byte data = (byte) entry.getValue(); byte data = (byte) entry.getValue();
if (entityID != info.getEntityID() && plugin.isShieldBlocking()) {
if ((data & 0x10) == 0x10) { if ((data & 0x10) == 0x10) {
if (validBlocking.contains(entityID)) { if (validBlocking.contains(entityID)) {
ItemSlotRewriter.ItemStack shield = new ItemSlotRewriter.ItemStack(); ItemSlotRewriter.ItemStack shield = new ItemSlotRewriter.ItemStack();
shield.id = 442; shield.id = 442;
shield.amount = 1; shield.amount = 1;
shield.data = 0; shield.data = 0;
sendSecondHandItem(entityID, shield); sendSecondHandItem(entityID, shield);
}
} else {
sendSecondHandItem(entityID, null);
} }
} else {
sendSecondHandItem(entityID, null);
} }
} }
} }

View File

@ -9,4 +9,6 @@ prevent-collision: true
# If the above is true, should we automatically team players until you do? # If the above is true, should we automatically team players until you do?
auto-team: true auto-team: true
# When enabled if certain metadata can't be read we won't tell you about it # When enabled if certain metadata can't be read we won't tell you about it
suppress-metadata-errors: false suppress-metadata-errors: false
# When enabled 1.9 will be able to block by using shields
shield-blocking: true