ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/AbstractFenceConnectionHand...

101 lines
4.6 KiB
Java
Raw Normal View History

/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
2024-01-01 12:39:45 +01:00
* Copyright (C) 2016-2024 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.viaversion.protocols.protocol1_13to1_12_2.blockconnections;
2018-11-17 15:45:37 +01:00
2021-04-26 21:35:29 +02:00
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockFace;
import com.viaversion.viaversion.api.minecraft.Position;
2021-04-26 21:16:10 +02:00
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.Arrays;
2018-11-17 15:45:37 +01:00
public abstract class AbstractFenceConnectionHandler extends ConnectionHandler {
private static final StairConnectionHandler STAIR_CONNECTION_HANDLER = new StairConnectionHandler();
private final IntSet blockStates = new IntOpenHashSet();
private final int[] connectedBlockStates = new int[statesSize()];
private final int blockConnectionsTypeId;
2018-11-17 15:45:37 +01:00
protected AbstractFenceConnectionHandler(String blockConnections) {
this.blockConnectionsTypeId = blockConnections != null ? BlockData.connectionTypeId(blockConnections) : -1;
Arrays.fill(connectedBlockStates, -1);
}
2018-11-17 15:45:37 +01:00
2024-02-10 17:58:54 +01:00
ConnectionData.ConnectorInitAction getInitAction(final String key) {
final AbstractFenceConnectionHandler handler = this;
return blockData -> {
if (key.equals(blockData.getMinecraftKey())) {
if (blockData.hasData("waterlogged") && blockData.getValue("waterlogged").equals("true")) {
return;
}
blockStates.add(blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
byte internalStateId = getStates(blockData);
connectedBlockStates[internalStateId] = blockData.getSavedBlockStateId();
2018-11-17 15:45:37 +01:00
}
};
2018-11-17 15:45:37 +01:00
}
protected byte getStates(WrappedBlockData blockData) {
byte states = 0;
if (blockData.getValue("east").equals("true")) states |= 1;
if (blockData.getValue("north").equals("true")) states |= 2;
if (blockData.getValue("south").equals("true")) states |= 4;
if (blockData.getValue("west").equals("true")) states |= 8;
return states;
}
protected byte getStates(UserConnection user, Position position, int blockState) {
byte states = 0;
2020-10-16 18:21:45 +02:00
boolean pre1_12 = user.getProtocolInfo().getServerProtocolVersion() < ProtocolVersion.v1_12.getVersion();
2019-03-22 19:18:24 +01:00
if (connects(BlockFace.EAST, getBlockData(user, position.getRelative(BlockFace.EAST)), pre1_12)) states |= 1;
if (connects(BlockFace.NORTH, getBlockData(user, position.getRelative(BlockFace.NORTH)), pre1_12)) states |= 2;
if (connects(BlockFace.SOUTH, getBlockData(user, position.getRelative(BlockFace.SOUTH)), pre1_12)) states |= 4;
if (connects(BlockFace.WEST, getBlockData(user, position.getRelative(BlockFace.WEST)), pre1_12)) states |= 8;
2018-11-17 15:45:37 +01:00
return states;
}
protected byte statesSize() {
return 16;
}
@Override
public int getBlockData(UserConnection user, Position position) {
return STAIR_CONNECTION_HANDLER.connect(user, position, super.getBlockData(user, position));
}
2018-11-17 15:45:37 +01:00
@Override
public int connect(UserConnection user, Position position, int blockState) {
final int newBlockState = connectedBlockStates[getStates(user, position, blockState)];
return newBlockState == -1 ? blockState : newBlockState;
2018-11-17 15:45:37 +01:00
}
2019-03-22 19:18:24 +01:00
protected boolean connects(BlockFace side, int blockState, boolean pre1_12) {
if (blockStates.contains(blockState)) return true;
if (blockConnectionsTypeId == -1) return false;
BlockData blockData = ConnectionData.blockConnectionData.get(blockState);
return blockData != null && blockData.connectsTo(blockConnectionsTypeId, side.opposite(), pre1_12);
}
public IntSet getBlockStates() {
return blockStates;
2018-11-17 15:45:37 +01:00
}
}