Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nbt multiplayer fixes #324

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions common/src/main/java/net/bettercombat/client/ClientNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public static void initializeHandlers() {
final var packet = Packets.AttackAnimation.read(buf);
client.execute(() -> {
var entity = client.world.getEntityById(packet.playerId());
if (entity instanceof PlayerEntity) {
//This addition check is not required, ive tested without it,
//but it probably retriggers the animation and could lead to issues on servers with higher pings
if (entity instanceof PlayerEntity player && player != client.player) {
if (packet.animationName().equals(Packets.AttackAnimation.StopSymbol)) {
((PlayerAttackAnimatable)entity).stopAttackAnimation(packet.length());
((PlayerAttackAnimatable) entity).stopAttackAnimation(packet.length());
} else {
((PlayerAttackAnimatable)entity).playAttackAnimation(packet.animationName(), packet.animatedHand(), packet.length(), packet.upswing());
((PlayerAttackAnimatable) entity).playAttackAnimation(packet.animationName(), packet.animatedHand(), packet.length(), packet.upswing());
}
}
});
Expand All @@ -36,7 +38,7 @@ public static void initializeHandlers() {

var soundEvent = Registries.SOUND_EVENT.get(new Identifier(packet.soundId()));
var configVolume = BetterCombatClient.config.weaponSwingSoundVolume;
var volume = packet.volume() * ((float)Math.min(Math.max(configVolume, 0), 100) / 100F);
var volume = packet.volume() * ((float) Math.min(Math.max(configVolume, 0), 100) / 100F);
client.world.playSound(
packet.x(),
packet.y(),
Expand Down
59 changes: 44 additions & 15 deletions common/src/main/java/net/bettercombat/logic/WeaponRegistry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.bettercombat.logic;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.mojang.logging.LogUtils;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static void loadAttributes(ResourceManager resourceManager) {
loadContainers(resourceManager);

// Resolving parents
containers.forEach( (itemId, container) -> {
containers.forEach((itemId, container) -> {
if (!Registries.ITEM.containsId(itemId)) {
return;
}
Expand Down Expand Up @@ -103,7 +104,7 @@ public static WeaponAttributes resolveAttributes(Identifier itemId, AttributesCo
}
}

var empty = new WeaponAttributes(0, null, null, false, null,null);
var empty = new WeaponAttributes(0, null, null, false, null, null);
var resolvedAttributes = resolutionChain
.stream()
.reduce(empty, (a, b) -> {
Expand Down Expand Up @@ -136,9 +137,15 @@ public static void resolveAndRegisterAttributes(Identifier itemId, AttributesCon
public static void encodeRegistry() {
PacketByteBuf buffer = PacketByteBufs.create();
var gson = new Gson();
var json = gson.toJson(registrations);

Map<String, JsonElement> data = new HashMap<>();
data.put("weapon_attributes", gson.toJsonTree(registrations));
data.put("attribute_containers", gson.toJsonTree(containers));

var json = gson.toJson(data);

if (BetterCombat.config.weapon_registry_logging) {
LOGGER.info("Weapon Attribute registry loaded: " + json);
LOGGER.info("Encoded registries: " + json);
}

List<String> chunks = new ArrayList<>();
Expand All @@ -148,33 +155,55 @@ public static void encodeRegistry() {
}

buffer.writeInt(chunks.size());
for (var chunk: chunks) {

for (var chunk : chunks) {
buffer.writeString(chunk);
}

LOGGER.info("Encoded Weapon Attribute registry size (with package overhead): " + buffer.readableBytes()
+ " bytes (in " + chunks.size() + " string chunks with the size of " + chunkSize + ")");
LOGGER.info("Encoded registries size (with package overhead): " + buffer.readableBytes()
+ " bytes (in " + chunks.size() + " string chunks with the size of " + chunkSize + ")");
encodedRegistrations = buffer;
}

public static void decodeRegistry(PacketByteBuf buffer) {
var chunkCount = buffer.readInt();
String json = "";
for (int i = 0; i < chunkCount; ++i) {
json = json.concat(buffer.readString());
var totalChunkCount = buffer.readInt();

StringBuilder jsonBuilder = new StringBuilder();

for (int i = 0; i < totalChunkCount; ++i) {
jsonBuilder.append(buffer.readString());
LOGGER.info("Decoded registry in chunk " + (i + 1));
}
LOGGER.info("Decoded Weapon Attribute registry in " + chunkCount + " string chunks");

String json = jsonBuilder.toString();

if (BetterCombat.config.weapon_registry_logging) {
LOGGER.info("Weapon Attribute registry received: " + json);
LOGGER.info("Decoded registries: " + json);
}

var gson = new Gson();
Type mapType = new TypeToken<Map<String, WeaponAttributes>>() {}.getType();
Map<String, WeaponAttributes> readRegistrations = gson.fromJson(json, mapType);
Type mapType = new TypeToken<Map<String, JsonElement>>() {
}.getType();
Type weaponAttributeType = new TypeToken<Map<String, WeaponAttributes>>() {
}.getType();
Type attributeContainerType = new TypeToken<Map<String, AttributesContainer>>() {
}.getType();
Map<String, JsonElement> data = gson.fromJson(json, mapType);

Map<String, WeaponAttributes> readRegistrations = gson.fromJson(data.get("weapon_attributes"), weaponAttributeType);
Map<Identifier, WeaponAttributes> newRegistrations = new HashMap();
readRegistrations.forEach((key, value) -> {
newRegistrations.put(new Identifier(key), value);
});

Map<String, AttributesContainer> readAttributes = gson.fromJson(data.get("attribute_containers"), attributeContainerType);
Map<Identifier, AttributesContainer> newAttributes = new HashMap();
readAttributes.forEach((key, value) -> {
newAttributes.put(new Identifier(key), value);
});

registrations = newRegistrations;
containers = newAttributes;
}

public static PacketByteBuf getEncodedRegistry() {
Expand Down
12 changes: 11 additions & 1 deletion common/src/main/java/net/bettercombat/network/ServerNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.SwordItem;
import net.minecraft.network.PacketByteBuf;
Expand All @@ -37,6 +38,7 @@
import net.minecraft.text.Text;
import org.slf4j.Logger;

import java.util.Collection;
import java.util.UUID;

public class ServerNetwork {
Expand All @@ -63,9 +65,17 @@ public static void initializeHandlers() {
}
final var packet = Packets.AttackAnimation.read(buf);
final var forwardBuffer = new Packets.AttackAnimation(player.getId(), packet.animatedHand(), packet.animationName(), packet.length(), packet.upswing()).write();
try {
//send info back for Replaymod Compat
if (ServerPlayNetworking.canSend(player, Packets.AttackAnimation.ID)) {
ServerPlayNetworking.send(player, Packets.AttackAnimation.ID, forwardBuffer);
}
} catch (Exception e){
e.printStackTrace();
}
PlayerLookup.tracking(player).forEach(serverPlayer -> {
try {
if (serverPlayer.getId() != player.getId() && ServerPlayNetworking.canSend(serverPlayer, Packets.AttackAnimation.ID)) {
if (ServerPlayNetworking.canSend(serverPlayer, Packets.AttackAnimation.ID)) {
ServerPlayNetworking.send(serverPlayer, Packets.AttackAnimation.ID, forwardBuffer);
}
} catch (Exception e){
Expand Down