From e9f834555f89ea6a54c7a36fb45a621969e9d094 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Wed, 25 May 2016 10:17:40 -0400 Subject: [PATCH] When ops make changes to the servers blacklist entries, the change is sent out to all currently logged in players so that they remain in sync with the servers entries. --- .../ee3/blacklist/BlacklistRegistry.java | 14 ++- .../ee3/command/CommandSetItemLearnable.java | 3 + .../command/CommandSetItemNotLearnable.java | 3 + .../command/CommandSetItemNotRecoverable.java | 3 + .../command/CommandSetItemRecoverable.java | 3 + .../message/MessageSetBlacklistEntry.java | 105 ++++++++++++++++++ 6 files changed, 126 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java b/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java index f2474f60..49d6f6b2 100644 --- a/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java +++ b/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java @@ -33,7 +33,7 @@ public class BlacklistRegistry { private final Set knowledgeBlacklist, exchangeBlacklist; public static File knowledgeBlacklistFile, exchangeBlacklistFile; - private transient boolean loadedFromServer; + private transient boolean shouldSave; /** * TODO Finish JavaDoc @@ -42,7 +42,7 @@ public class BlacklistRegistry { knowledgeBlacklist = new TreeSet<>(); exchangeBlacklist = new TreeSet<>(); - loadedFromServer = false; + shouldSave = true; } /** @@ -161,6 +161,10 @@ public class BlacklistRegistry { } } + public void setShouldSave(boolean shouldSave) { + this.shouldSave = shouldSave; + } + /** * TODO Finish JavaDoc */ @@ -185,7 +189,7 @@ public class BlacklistRegistry { if (blacklist != null && blacklistSet != null) { - loadedFromServer = true; + setShouldSave(false); if (blacklist == Blacklist.KNOWLEDGE) { LogHelper.info("Received {} player knowledge blacklist entries from server", blacklistSet.size()); @@ -207,7 +211,7 @@ public class BlacklistRegistry { */ public void save(Blacklist blacklist) { - if (!loadedFromServer) { + if (shouldSave) { if (blacklist == Blacklist.KNOWLEDGE) { LogHelper.trace(BLACKLIST_MARKER, "Saving player knowledge blacklist to {}", knowledgeBlacklistFile.getAbsolutePath()); SerializationHelper.writeJsonFile(knowledgeBlacklistFile, SerializationHelper.GSON.toJson(knowledgeBlacklist)); @@ -224,7 +228,7 @@ public class BlacklistRegistry { */ public void saveAll() { - if (!loadedFromServer) { + if (shouldSave) { LogHelper.trace(BLACKLIST_MARKER, "Saving all blacklists to disk", exchangeBlacklistFile.getAbsolutePath()); SerializationHelper.writeJsonFile(knowledgeBlacklistFile, SerializationHelper.GSON.toJson(knowledgeBlacklist)); SerializationHelper.writeJsonFile(exchangeBlacklistFile, SerializationHelper.GSON.toJson(exchangeBlacklist)); diff --git a/src/main/java/com/pahimar/ee3/command/CommandSetItemLearnable.java b/src/main/java/com/pahimar/ee3/command/CommandSetItemLearnable.java index 3105d49a..e1e7a266 100644 --- a/src/main/java/com/pahimar/ee3/command/CommandSetItemLearnable.java +++ b/src/main/java/com/pahimar/ee3/command/CommandSetItemLearnable.java @@ -1,6 +1,8 @@ package com.pahimar.ee3.command; import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy; +import com.pahimar.ee3.network.PacketHandler; +import com.pahimar.ee3.network.message.MessageSetBlacklistEntry; import com.pahimar.ee3.reference.Messages; import com.pahimar.ee3.reference.Names; import net.minecraft.command.CommandBase; @@ -77,6 +79,7 @@ public class CommandSetItemLearnable extends CommandBase } BlacklistRegistryProxy.removeFromBlacklist(itemStack, BlacklistRegistryProxy.Blacklist.KNOWLEDGE); + PacketHandler.INSTANCE.sendToAll(new MessageSetBlacklistEntry(itemStack, BlacklistRegistryProxy.Blacklist.KNOWLEDGE, false)); func_152373_a(commandSender, this, Messages.Commands.SET_ITEM_LEARNABLE_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()}); } } diff --git a/src/main/java/com/pahimar/ee3/command/CommandSetItemNotLearnable.java b/src/main/java/com/pahimar/ee3/command/CommandSetItemNotLearnable.java index 5302cec1..53f150a8 100644 --- a/src/main/java/com/pahimar/ee3/command/CommandSetItemNotLearnable.java +++ b/src/main/java/com/pahimar/ee3/command/CommandSetItemNotLearnable.java @@ -1,6 +1,8 @@ package com.pahimar.ee3.command; import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy; +import com.pahimar.ee3.network.PacketHandler; +import com.pahimar.ee3.network.message.MessageSetBlacklistEntry; import com.pahimar.ee3.reference.Messages; import com.pahimar.ee3.reference.Names; import net.minecraft.command.CommandBase; @@ -77,6 +79,7 @@ public class CommandSetItemNotLearnable extends CommandBase } BlacklistRegistryProxy.addToBlacklist(itemStack, BlacklistRegistryProxy.Blacklist.KNOWLEDGE); + PacketHandler.INSTANCE.sendToAll(new MessageSetBlacklistEntry(itemStack, BlacklistRegistryProxy.Blacklist.KNOWLEDGE, true)); func_152373_a(commandSender, this, Messages.Commands.SET_ITEM_NOT_LEARNABLE_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()}); } } diff --git a/src/main/java/com/pahimar/ee3/command/CommandSetItemNotRecoverable.java b/src/main/java/com/pahimar/ee3/command/CommandSetItemNotRecoverable.java index 2b4f4d5b..4321c07f 100644 --- a/src/main/java/com/pahimar/ee3/command/CommandSetItemNotRecoverable.java +++ b/src/main/java/com/pahimar/ee3/command/CommandSetItemNotRecoverable.java @@ -1,6 +1,8 @@ package com.pahimar.ee3.command; import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy; +import com.pahimar.ee3.network.PacketHandler; +import com.pahimar.ee3.network.message.MessageSetBlacklistEntry; import com.pahimar.ee3.reference.Messages; import com.pahimar.ee3.reference.Names; import net.minecraft.command.CommandBase; @@ -77,6 +79,7 @@ public class CommandSetItemNotRecoverable extends CommandBase } BlacklistRegistryProxy.addToBlacklist(itemStack, BlacklistRegistryProxy.Blacklist.EXCHANGE); + PacketHandler.INSTANCE.sendToAll(new MessageSetBlacklistEntry(itemStack, BlacklistRegistryProxy.Blacklist.EXCHANGE, true)); func_152373_a(commandSender, this, Messages.Commands.SET_ITEM_NOT_RECOVERABLE_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()}); } } diff --git a/src/main/java/com/pahimar/ee3/command/CommandSetItemRecoverable.java b/src/main/java/com/pahimar/ee3/command/CommandSetItemRecoverable.java index b30a6010..cb6b2ffa 100644 --- a/src/main/java/com/pahimar/ee3/command/CommandSetItemRecoverable.java +++ b/src/main/java/com/pahimar/ee3/command/CommandSetItemRecoverable.java @@ -1,6 +1,8 @@ package com.pahimar.ee3.command; import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy; +import com.pahimar.ee3.network.PacketHandler; +import com.pahimar.ee3.network.message.MessageSetBlacklistEntry; import com.pahimar.ee3.reference.Messages; import com.pahimar.ee3.reference.Names; import net.minecraft.command.CommandBase; @@ -77,6 +79,7 @@ public class CommandSetItemRecoverable extends CommandBase } BlacklistRegistryProxy.removeFromBlacklist(itemStack, BlacklistRegistryProxy.Blacklist.EXCHANGE); + PacketHandler.INSTANCE.sendToAll(new MessageSetBlacklistEntry(itemStack, BlacklistRegistryProxy.Blacklist.EXCHANGE, true)); func_152373_a(commandSender, this, Messages.Commands.SET_ITEM_RECOVERABLE_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()}); } } diff --git a/src/main/java/com/pahimar/ee3/network/message/MessageSetBlacklistEntry.java b/src/main/java/com/pahimar/ee3/network/message/MessageSetBlacklistEntry.java index fbe23f36..a1a35327 100644 --- a/src/main/java/com/pahimar/ee3/network/message/MessageSetBlacklistEntry.java +++ b/src/main/java/com/pahimar/ee3/network/message/MessageSetBlacklistEntry.java @@ -1,26 +1,131 @@ package com.pahimar.ee3.network.message; +import com.google.gson.JsonParseException; +import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy; +import com.pahimar.ee3.blacklist.BlacklistRegistry; +import com.pahimar.ee3.exchange.WrappedStack; +import com.pahimar.ee3.util.CompressionHelper; +import com.pahimar.ee3.util.LogHelper; +import com.pahimar.ee3.util.SerializationHelper; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; import io.netty.buffer.ByteBuf; +import static com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy.Blacklist; + public class MessageSetBlacklistEntry implements IMessage, IMessageHandler { + public Blacklist blacklist; + public boolean isBlacklistAction; + public WrappedStack wrappedStack; + public MessageSetBlacklistEntry() { } + public MessageSetBlacklistEntry(Object object, Blacklist blacklist, boolean isBlacklistAction) { + + if (WrappedStack.canBeWrapped(object)) { + this.wrappedStack = WrappedStack.wrap(object, 1); + } + else { + wrappedStack = null; + } + this.blacklist = blacklist; + this.isBlacklistAction = isBlacklistAction; + } + @Override public void fromBytes(ByteBuf buf) { + + int blacklistOrdinal = buf.readInt(); + + if (blacklistOrdinal >= 0) { + + if (blacklistOrdinal == 0) { + blacklist = Blacklist.KNOWLEDGE; + } + else if (blacklistOrdinal == 1) { + blacklist = Blacklist.EXCHANGE; + } + else { + blacklist = null; + } + + if (blacklist != null) { + + isBlacklistAction = buf.readBoolean(); + int compressedJsonLength = buf.readInt(); + + if (compressedJsonLength != 0) { + + byte[] compressedWrappedStack = buf.readBytes(compressedJsonLength).array(); + + if (compressedWrappedStack != null) { + + String jsonWrappedStack = CompressionHelper.decompress(compressedWrappedStack); + + try { + wrappedStack = SerializationHelper.GSON.fromJson(jsonWrappedStack, WrappedStack.class); + } + catch (JsonParseException e) { + LogHelper.warn("Failed to receive {} blacklist data from server", blacklist); + wrappedStack = null; + } + } + else { + wrappedStack = null; + } + } + else { + wrappedStack = null; + } + } + } + else { + blacklist = null; + isBlacklistAction = false; + wrappedStack = null; + } } @Override public void toBytes(ByteBuf buf) { + + if (blacklist != null) { + + buf.writeBoolean(isBlacklistAction); + + if (wrappedStack != null && wrappedStack.getWrappedObject() != null) { + + byte[] compressedWrappedStack = CompressionHelper.compress(SerializationHelper.GSON.toJson(wrappedStack)); + buf.writeInt(compressedWrappedStack.length); + buf.writeBytes(compressedWrappedStack); + } + else { + buf.writeInt(0); + } + } + else { + buf.writeInt(-1); + } } @Override public IMessage onMessage(MessageSetBlacklistEntry message, MessageContext ctx) { + if (message.blacklist != null && message.wrappedStack != null) { + + if (message.isBlacklistAction) { + BlacklistRegistryProxy.addToBlacklist(message.wrappedStack, message.blacklist); + BlacklistRegistry.INSTANCE.setShouldSave(false); + } + else { + BlacklistRegistryProxy.removeFromBlacklist(message.wrappedStack, message.blacklist); + BlacklistRegistry.INSTANCE.setShouldSave(false); + } + } + return null; } }