-
-
Notifications
You must be signed in to change notification settings - Fork 93
Implement 1.9->1.8 command block translation #692
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
Open
Beaness
wants to merge
1
commit into
ViaVersion:master
Choose a base branch
from
Beaness:commandblocks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
common/src/main/java/com/viaversion/viarewind/protocol/v1_9to1_8/data/CommandBlockState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* | ||
| * This file is part of ViaRewind - https://github.com/ViaVersion/ViaRewind | ||
| * Copyright (C) 2018-2026 ViaVersion and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.viaversion.viarewind.protocol.v1_9to1_8.data; | ||
|
|
||
| import com.viaversion.nbt.tag.CompoundTag; | ||
| import com.viaversion.nbt.tag.NumberTag; | ||
| import com.viaversion.nbt.tag.StringTag; | ||
|
|
||
| public final class CommandBlockState { | ||
|
|
||
| public static final int COMMAND_BLOCK_COMMAND_LIMIT = 32500; | ||
|
|
||
| private static final String IMPULSE = "IMPULSE"; | ||
| private static final String REPEAT = "REPEAT"; | ||
| private static final String CHAIN = "CHAIN"; | ||
| private static final String CONDITIONAL = "CONDITIONAL"; | ||
| private static final String UNCONDITIONAL = "UNCONDITIONAL"; | ||
| private static final String ALWAYS_ACTIVE = "ALWAYSACTIVE"; | ||
| private static final String REDSTONE = "REDSTONE"; | ||
|
|
||
| private CommandBlockState() { | ||
| } | ||
|
|
||
| public static boolean isCommandBlock(final int blockState) { | ||
| return isImpulse(blockState) || isRepeat(blockState) || isChain(blockState); | ||
| } | ||
|
|
||
| public static void decorateCommand(final CompoundTag tag, final int blockState) { | ||
| if (tag == null || !isCommandBlock(blockState)) { | ||
| return; | ||
| } | ||
|
|
||
| final StringTag commandTag = tag.getStringTag("Command"); | ||
| if (commandTag == null) { | ||
| return; | ||
| } | ||
|
|
||
| final Mode mode = mode(blockState); | ||
| final boolean conditional = (blockState & 8) != 0; | ||
| final boolean automatic = booleanTag(tag, "auto"); | ||
| if (mode == Mode.REDSTONE && !conditional && !automatic) { | ||
| return; | ||
| } | ||
|
|
||
| final String prefix = mode.prefix + ' ' + (conditional ? CONDITIONAL : UNCONDITIONAL) + ' ' + (automatic ? ALWAYS_ACTIVE : REDSTONE) + ' '; | ||
| final String command = commandTag.getValue(); | ||
| if (!parseDecoratedCommand(command).prefixed() && prefix.length() + command.length() <= COMMAND_BLOCK_COMMAND_LIMIT) { | ||
| commandTag.setValue(prefix + command); | ||
| } | ||
| } | ||
|
|
||
| public static ParsedCommand parseDecoratedCommand(final String command) { | ||
| final int first = command.indexOf(' '); | ||
| if (first == -1) { | ||
| return ParsedCommand.defaultState(command); | ||
| } | ||
| final int second = command.indexOf(' ', first + 1); | ||
| if (second == -1) { | ||
| return ParsedCommand.defaultState(command); | ||
| } | ||
| final int third = command.indexOf(' ', second + 1); | ||
| if (third == -1) { | ||
| return ParsedCommand.defaultState(command); | ||
| } | ||
|
|
||
| final Mode mode = Mode.fromPrefix(command.substring(0, first)); | ||
| if (mode == null) { | ||
| return ParsedCommand.defaultState(command); | ||
| } | ||
|
|
||
| final String conditionalToken = command.substring(first + 1, second); | ||
| final boolean conditional; | ||
| if (CONDITIONAL.equalsIgnoreCase(conditionalToken)) { | ||
| conditional = true; | ||
| } else if (UNCONDITIONAL.equalsIgnoreCase(conditionalToken)) { | ||
| conditional = false; | ||
| } else { | ||
| return ParsedCommand.defaultState(command); | ||
| } | ||
|
|
||
| final String automaticToken = command.substring(second + 1, third); | ||
| final boolean automatic; | ||
| if (ALWAYS_ACTIVE.equalsIgnoreCase(automaticToken)) { | ||
| automatic = true; | ||
| } else if (REDSTONE.equalsIgnoreCase(automaticToken)) { | ||
| automatic = false; | ||
| } else { | ||
| return ParsedCommand.defaultState(command); | ||
| } | ||
|
|
||
| return new ParsedCommand(command.substring(third + 1), mode.serverName, conditional, automatic, true); | ||
| } | ||
|
|
||
| private static boolean booleanTag(final CompoundTag tag, final String key) { | ||
| final NumberTag numberTag = tag.getNumberTag(key); | ||
| return numberTag != null && numberTag.asByte() != 0; | ||
| } | ||
|
|
||
| private static Mode mode(final int blockState) { | ||
| if (isRepeat(blockState)) { | ||
| return Mode.AUTO; | ||
| } | ||
| if (isChain(blockState)) { | ||
| return Mode.SEQUENCE; | ||
| } | ||
| return Mode.REDSTONE; | ||
| } | ||
|
|
||
| private static boolean isImpulse(final int blockState) { | ||
| return blockState >= 2192 && blockState <= 2207; | ||
| } | ||
|
|
||
| private static boolean isRepeat(final int blockState) { | ||
| return blockState >= 3360 && blockState <= 3375; | ||
| } | ||
|
|
||
| private static boolean isChain(final int blockState) { | ||
| return blockState >= 3376 && blockState <= 3391; | ||
| } | ||
|
|
||
| public record ParsedCommand(String command, String mode, boolean conditional, boolean automatic, boolean prefixed) { | ||
|
|
||
| private static ParsedCommand defaultState(final String command) { | ||
| return new ParsedCommand(command, Mode.REDSTONE.serverName, false, false, false); | ||
| } | ||
| } | ||
|
|
||
| private enum Mode { | ||
| REDSTONE(IMPULSE, "REDSTONE"), | ||
| AUTO(REPEAT, "AUTO"), | ||
| SEQUENCE(CHAIN, "SEQUENCE"); | ||
|
|
||
| private final String prefix; | ||
| private final String serverName; | ||
|
|
||
| Mode(final String prefix, final String serverName) { | ||
| this.prefix = prefix; | ||
| this.serverName = serverName; | ||
| } | ||
|
|
||
| private static Mode fromPrefix(final String prefix) { | ||
| for (final Mode mode : values()) { | ||
| if (mode.prefix.equalsIgnoreCase(prefix)) { | ||
| return mode; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,11 @@ | |
| import com.viaversion.viarewind.api.rewriter.VRBlockItemRewriter; | ||
| import com.viaversion.viarewind.protocol.v1_9to1_8.Protocol1_9To1_8; | ||
| import com.viaversion.viarewind.protocol.v1_9to1_8.data.PotionIdMappings1_8; | ||
| import com.viaversion.viarewind.protocol.v1_9to1_8.storage.CommandBlockStateStorage; | ||
| import com.viaversion.viarewind.protocol.v1_9to1_8.storage.WindowTracker; | ||
| import com.viaversion.viaversion.api.connection.UserConnection; | ||
| import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; | ||
| import com.viaversion.viaversion.api.minecraft.BlockPosition; | ||
| import com.viaversion.viaversion.api.minecraft.item.Item; | ||
| import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; | ||
| import com.viaversion.viaversion.api.type.Types; | ||
|
|
@@ -59,8 +62,8 @@ public BlockItemPacketRewriter1_9(Protocol1_9To1_8 protocol) { | |
|
|
||
| @Override | ||
| protected void registerPackets() { | ||
| registerBlockChange(ClientboundPackets1_9.BLOCK_UPDATE); | ||
| registerMultiBlockChange(ClientboundPackets1_9.CHUNK_BLOCKS_UPDATE); | ||
| registerBlockChangeWithCommandBlockStorage(ClientboundPackets1_9.BLOCK_UPDATE); | ||
| registerMultiBlockChangeWithCommandBlockStorage(ClientboundPackets1_9.CHUNK_BLOCKS_UPDATE); | ||
| registerSetCreativeModeSlot(ServerboundPackets1_8.SET_CREATIVE_MODE_SLOT); | ||
|
|
||
| protocol.registerClientbound(ClientboundPackets1_9.CONTAINER_CLOSE, wrapper -> { | ||
|
|
@@ -184,6 +187,46 @@ public void register() { | |
| }); | ||
| } | ||
|
|
||
| private void registerBlockChangeWithCommandBlockStorage(final ClientboundPackets1_9 packetType) { | ||
| protocol.registerClientbound(packetType, new PacketHandlers() { | ||
| @Override | ||
| public void register() { | ||
| map(Types.BLOCK_POSITION1_8); // Block Position | ||
| map(Types.VAR_INT); // Block | ||
|
|
||
| handler(wrapper -> { | ||
| final int blockState = wrapper.get(Types.VAR_INT, 0); | ||
| wrapper.user().get(CommandBlockStateStorage.class).storeOrRemove(wrapper.get(Types.BLOCK_POSITION1_8, 0), blockState); | ||
| wrapper.set(Types.VAR_INT, 0, handleBlockId(blockState)); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void registerMultiBlockChangeWithCommandBlockStorage(final ClientboundPackets1_9 packetType) { | ||
| protocol.registerClientbound(packetType, new PacketHandlers() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, just inline. Also would like to prevent PacketHandlers API from being used; Just a direct wrapper -> {} lambda with passthrough instead of map (and then storing in a field e.g. final int chunkX = wrapper.passthrough(Types.INT);) |
||
| @Override | ||
| public void register() { | ||
| map(Types.INT); // Chunk X | ||
| map(Types.INT); // Chunk Z | ||
| map(Types.BLOCK_CHANGE_ARRAY); | ||
|
|
||
| handler(wrapper -> { | ||
| final int chunkX = wrapper.get(Types.INT, 0); | ||
| final int chunkZ = wrapper.get(Types.INT, 1); | ||
| final CommandBlockStateStorage storage = wrapper.user().get(CommandBlockStateStorage.class); | ||
|
|
||
| for (BlockChangeRecord record : wrapper.get(Types.BLOCK_CHANGE_ARRAY, 0)) { | ||
| final int blockState = record.getBlockId(); | ||
| final BlockPosition position = new BlockPosition((chunkX << 4) + record.getSectionX(), record.getY(), (chunkZ << 4) + record.getSectionZ()); | ||
| storage.storeOrRemove(position, blockState); | ||
| record.setBlockId(handleBlockId(blockState)); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| protected void registerRewrites() { | ||
| enchantmentRewriter = new LegacyEnchantmentRewriter(nbtTagName()); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can just be inlined, the helper methods only exist in rewriter base classes to reduce duplicated code.