From f2412bca13faaa136613727e0b75812caf35338b Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 3 Aug 2018 11:12:03 +1000 Subject: [PATCH] Add nbt stripping --- .../java/com/boydti/fawe/config/Settings.java | 12 +++++ .../com/boydti/fawe/object/FaweLimit.java | 14 +++-- .../fawe/object/extent/StripNBTExtent.java | 49 +++++++++++++++++ .../java/com/sk89q/worldedit/EditSession.java | 4 ++ .../worldedit/command/HistoryCommands.java | 53 ++++++++++++++++++- .../com/sk89q/worldedit/extent/Extent.java | 2 + 6 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/com/boydti/fawe/object/extent/StripNBTExtent.java diff --git a/core/src/main/java/com/boydti/fawe/config/Settings.java b/core/src/main/java/com/boydti/fawe/config/Settings.java index 6191785f..dd0d22eb 100644 --- a/core/src/main/java/com/boydti/fawe/config/Settings.java +++ b/core/src/main/java/com/boydti/fawe/config/Settings.java @@ -160,6 +160,10 @@ public class Settings extends Config { "Should large edits require confirmation (>16384 chunks)", }) public boolean CONFIRM_LARGE = true; + @Comment({ + "List of blocks to strip nbt from", + }) + public List STRIP_NBT = new ArrayList<>(); } public static class HISTORY { @@ -476,6 +480,14 @@ public class Settings extends Config { limit.SPEED_REDUCTION = Math.min(limit.SPEED_REDUCTION, newLimit.SPEED_REDUCTION); limit.FAST_PLACEMENT |= newLimit.FAST_PLACEMENT; limit.CONFIRM_LARGE &= newLimit.CONFIRM_LARGE; + if (limit.STRIP_NBT == null) limit.STRIP_NBT = newLimit.STRIP_NBT.isEmpty() ? Collections.emptySet() : new HashSet<>(newLimit.STRIP_NBT); + else if (limit.STRIP_NBT.isEmpty() || newLimit.STRIP_NBT.isEmpty()) { + limit.STRIP_NBT = Collections.emptySet(); + } else { + limit.STRIP_NBT = new HashSet<>(limit.STRIP_NBT); + limit.STRIP_NBT.retainAll(newLimit.STRIP_NBT); + if (limit.STRIP_NBT.isEmpty()) limit.STRIP_NBT = Collections.emptySet(); + } } } return limit; diff --git a/core/src/main/java/com/boydti/fawe/object/FaweLimit.java b/core/src/main/java/com/boydti/fawe/object/FaweLimit.java index 21584e15..bd1fe2b5 100644 --- a/core/src/main/java/com/boydti/fawe/object/FaweLimit.java +++ b/core/src/main/java/com/boydti/fawe/object/FaweLimit.java @@ -1,5 +1,10 @@ package com.boydti.fawe.object; +import com.sk89q.worldedit.blocks.BaseBlock; + +import java.util.Collections; +import java.util.Set; + /** * Created by Jesse on 4/5/2016. */ @@ -17,7 +22,7 @@ public class FaweLimit { public int SPEED_REDUCTION = Integer.MAX_VALUE; public boolean FAST_PLACEMENT = false; public boolean CONFIRM_LARGE = true; -// public boolean[] STRIP_NBT = null; + public Set STRIP_NBT = null; public static FaweLimit MAX; @@ -66,7 +71,7 @@ public class FaweLimit { MAX.MAX_EXPRESSION_MS = 50; MAX.FAST_PLACEMENT = true; MAX.CONFIRM_LARGE = true; -// MAX.STRIP_NBT = null; + MAX.STRIP_NBT = null; } public boolean MAX_CHANGES() { @@ -103,7 +108,8 @@ public class FaweLimit { MAX_HISTORY == Integer.MAX_VALUE && INVENTORY_MODE == 0 && SPEED_REDUCTION == 0 && - FAST_PLACEMENT == true; + FAST_PLACEMENT == true && + (STRIP_NBT == null || STRIP_NBT.isEmpty()); } public void set(FaweLimit limit) { @@ -119,6 +125,7 @@ public class FaweLimit { SPEED_REDUCTION = limit.SPEED_REDUCTION; FAST_PLACEMENT = limit.FAST_PLACEMENT; CONFIRM_LARGE = limit.CONFIRM_LARGE; + STRIP_NBT = limit.STRIP_NBT; } public FaweLimit copy() { @@ -135,6 +142,7 @@ public class FaweLimit { limit.MAX_HISTORY = MAX_HISTORY; limit.FAST_PLACEMENT = FAST_PLACEMENT; limit.CONFIRM_LARGE = CONFIRM_LARGE; + limit.STRIP_NBT = STRIP_NBT; return limit; } diff --git a/core/src/main/java/com/boydti/fawe/object/extent/StripNBTExtent.java b/core/src/main/java/com/boydti/fawe/object/extent/StripNBTExtent.java new file mode 100644 index 00000000..aaa6855f --- /dev/null +++ b/core/src/main/java/com/boydti/fawe/object/extent/StripNBTExtent.java @@ -0,0 +1,49 @@ +package com.boydti.fawe.object.extent; + +import com.boydti.fawe.util.ReflectionUtils; +import com.sk89q.jnbt.CompoundTag; +import com.sk89q.jnbt.Tag; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.extent.AbstractDelegateExtent; +import com.sk89q.worldedit.extent.Extent; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class StripNBTExtent extends AbstractDelegateExtent { + private final String[] strip; + + /** + * Create a new instance. + * + * @param extent the extent + */ + public StripNBTExtent(Extent extent, Set strip) { + super(extent); + this. + strip = strip.toArray(new String[strip.size()]); + } + + @Override + public boolean setBlock(Vector location, BaseBlock block) throws WorldEditException { + return super.setBlock(location, stripNBT(block)); + } + + @Override + public boolean setBlock(int x, int y, int z, BaseBlock block) throws WorldEditException { + return super.setBlock(x, y, z, stripNBT(block)); + } + + public BaseBlock stripNBT(BaseBlock block) { + if (!block.hasNbtData()) return block; + CompoundTag nbt = block.getNbtData(); + Map value = nbt.getValue(); + for (String key : strip) { + value.remove(key); + } + return block; + } +} diff --git a/core/src/main/java/com/sk89q/worldedit/EditSession.java b/core/src/main/java/com/sk89q/worldedit/EditSession.java index 22d13c9e..baa8fae3 100644 --- a/core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -56,6 +56,7 @@ import com.sk89q.worldedit.extent.ChangeSetExtent; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.MaskingExtent; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; +import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBagExtent; import com.sk89q.worldedit.extent.world.SurvivalModeExtent; @@ -317,6 +318,9 @@ public class EditSession extends AbstractDelegateExtent implements HasFaweQueue, } else { this.extent = new HeightBoundExtent(this.extent, this.limit, 0, maxY); } + if (this.limit.STRIP_NBT != null && !this.limit.STRIP_NBT.isEmpty()) { + this.extent = new StripNBTExtent(this.extent, this.limit.STRIP_NBT); + } this.extent = wrapExtent(this.extent, bus, event, Stage.BEFORE_HISTORY); setExtent(this.extent); } diff --git a/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java b/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java index e2e07d6d..997a1e5d 100644 --- a/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java +++ b/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java @@ -62,6 +62,21 @@ public class HistoryCommands extends MethodCommands { super(worldEdit); } + + @Command( + aliases = {"/frb", "frb", "fawerollback", "/fawerollback", "/rollback"}, + usage = " ", + desc = "Undo a specific edit. " + + " - The time uses s, m, h, d, y.\n" + + " - Import from disk: /frb #import", + min = 1, + max = 3 + ) + @CommandPermissions("worldedit.history.rollback") + public void faweRollback(final Player player, LocalSession session, final String user, @Optional("0") @Range(min = 0) int radius, @Optional("0") String time) throws WorldEditException { + + } + @Command( aliases = {"/frb", "frb", "fawerollback", "/fawerollback", "/rollback"}, usage = " ", @@ -199,8 +214,42 @@ public class HistoryCommands extends MethodCommands { ); } - private void rollBack(DiskStorageHistory file) { - + @Command( + aliases = {"/fawerestore", "/frestore"}, + usage = " ", + desc = "Redo a specific edit. " + + " - The time uses s, m, h, d, y.\n" + + " - Import from disk: /frb #import", + min = 1, + max = 3 + ) + @CommandPermissions("worldedit.history.rollback") + private void restore(DiskStorageHistory file) { + int times = Math.max(1, context.getInteger(0, 1)); + if (times > 50) { + FawePlayer.wrap(player).checkConfirmation(getArguments(context), times, 50); + } + for (int i = 0; i < times; ++i) { + EditSession undone; + if (context.argsLength() < 2) { + undone = session.undo(session.getBlockBag(player), player); + } else { + player.checkPermission("worldedit.history.undo.other"); + LocalSession sess = worldEdit.getSession(context.getString(1)); + if (sess == null) { + BBC.COMMAND_HISTORY_OTHER_ERROR.send(player, context.getString(1)); + break; + } + undone = sess.undo(session.getBlockBag(player), player); + } + if (undone != null) { + BBC.COMMAND_UNDO_SUCCESS.send(player); + worldEdit.flushBlockBag(player, undone); + } else { + BBC.COMMAND_UNDO_ERROR.send(player); + break; + } + } } @Command( diff --git a/core/src/main/java/com/sk89q/worldedit/extent/Extent.java b/core/src/main/java/com/sk89q/worldedit/extent/Extent.java index 0e2e3411..9d0a011c 100644 --- a/core/src/main/java/com/sk89q/worldedit/extent/Extent.java +++ b/core/src/main/java/com/sk89q/worldedit/extent/Extent.java @@ -3,12 +3,14 @@ package com.sk89q.worldedit.extent; import com.boydti.fawe.FaweCache; import com.boydti.fawe.jnbt.anvil.generator.*; import com.boydti.fawe.object.PseudoRandom; +import com.boydti.fawe.object.clipboard.WorldCopyClipboard; import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockType; import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.Entity; +import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.pattern.Pattern;