Implement shoulder indication

This commit is contained in:
Matsv 2017-06-13 20:48:02 +02:00
parent d330b64bba
commit 469936308c
No known key found for this signature in database
GPG Key ID: 97CEC2A2EA31350F
3 changed files with 135 additions and 3 deletions

View File

@ -13,6 +13,7 @@ package nl.matsv.viabackwards.protocol.protocol1_12to1_11_1;
import lombok.Getter;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.data.ShoulderTracker;
import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.packets.*;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
@ -42,6 +43,8 @@ public class Protocol1_11_1To1_12 extends BackwardsProtocol {
if (!user.has(EntityTracker.class))
user.put(new EntityTracker(user));
user.put(new ShoulderTracker(user));
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
}

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2016 Matsv
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.Protocol1_12To1_11_1;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
@Getter
@Setter
@ToString
public class ShoulderTracker extends StoredObject {
private int entityId;
private String leftShoulder;
private String rightShoulder;
public ShoulderTracker(UserConnection user) {
super(user);
}
public void update() {
PacketWrapper wrapper = new PacketWrapper(0x0F, null, getUser());
wrapper.write(Type.STRING, Protocol1_9TO1_8.fixJson(generateString()));
wrapper.write(Type.BYTE, (byte) 2);
try {
wrapper.send(Protocol1_12To1_11_1.class);
} catch (Exception e) {
System.out.println("Failed to send the shoulder indication");
e.printStackTrace();
}
}
// Does actionbar not support json colors? :(
private String generateString() {
StringBuilder builder = new StringBuilder();
// Empty spaces because the non-json formatting is weird
builder.append(" ");
if (leftShoulder == null)
builder.append(ChatColor.RED).append(ChatColor.BOLD).append("Nothing");
else
builder.append(ChatColor.DARK_GREEN).append(ChatColor.BOLD).append(getName(leftShoulder));
builder.append(ChatColor.DARK_GRAY).append(ChatColor.BOLD).append(" <- ")
.append(ChatColor.GRAY).append(ChatColor.BOLD).append("Shoulders")
.append(ChatColor.DARK_GRAY).append(ChatColor.BOLD).append(" -> ");
if (rightShoulder == null)
builder.append(ChatColor.RED).append(ChatColor.BOLD).append("Nothing");
else
builder.append(ChatColor.DARK_GREEN).append(ChatColor.BOLD).append(getName(rightShoulder));
return builder.toString();
}
private String getName(String current) {
if (current.startsWith("minecraft:"))
current = current.substring(10);
String[] array = current.split("_");
StringBuilder builder = new StringBuilder();
for (String s : array) {
builder.append(s.substring(0, 1).toUpperCase())
.append(s.substring(1))
.append(" ");
}
return builder.toString();
}
}

View File

@ -19,6 +19,7 @@ import nl.matsv.viabackwards.api.exceptions.RemovedValueException;
import nl.matsv.viabackwards.api.rewriters.EntityRewriter;
import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.Protocol1_11_1To1_12;
import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.data.ParrotStorage;
import nl.matsv.viabackwards.protocol.protocol1_12to1_11_1.data.ShoulderTracker;
import nl.matsv.viabackwards.utils.Block;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
@ -30,6 +31,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_12;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import java.util.Optional;
@ -293,6 +295,14 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ShoulderTracker tracker = wrapper.user().get(ShoulderTracker.class);
tracker.setEntityId(wrapper.get(Type.INT, 0));
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
@ -403,9 +413,41 @@ public class EntityPackets1_12 extends EntityRewriter<Protocol1_11_1To1_12> {
registerMetaHandler().filter(EntityType.PARROT, 15).removed(); // Variant
// Left shoulder entity data
registerMetaHandler().filter(EntityType.PLAYER, 15).removed();
// Right shoulder entity data
registerMetaHandler().filter(EntityType.PLAYER, 16).removed();
registerMetaHandler().filter(EntityType.PLAYER, 15).handle(e -> {
CompoundTag tag = (CompoundTag) e.getData().getValue();
ShoulderTracker tracker = e.getUser().get(ShoulderTracker.class);
if (tag.isEmpty() && tracker.getLeftShoulder() != null) {
tracker.setLeftShoulder(null);
tracker.update();
} else if (tag.contains("id") && e.getEntity().getEntityId() == tracker.getEntityId()) {
String id = (String) tag.get("id").getValue();
if (tracker.getLeftShoulder() == null || !tracker.getLeftShoulder().equals(id)) {
tracker.setLeftShoulder(id);
tracker.update();
}
}
throw new RemovedValueException();
});
// Right shoulder entity data
registerMetaHandler().filter(EntityType.PLAYER, 16).handle(e -> {
CompoundTag tag = (CompoundTag) e.getData().getValue();
ShoulderTracker tracker = e.getUser().get(ShoulderTracker.class);
if (tag.isEmpty() && tracker.getRightShoulder() != null) {
tracker.setRightShoulder(null);
tracker.update();
} else if (tag.contains("id") && e.getEntity().getEntityId() == tracker.getEntityId()) {
String id = (String) tag.get("id").getValue();
if (tracker.getRightShoulder() == null || !tracker.getRightShoulder().equals(id)) {
tracker.setRightShoulder(id);
tracker.update();
}
}
throw new RemovedValueException();
});
}
}