Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 93a3de5
Author: bundabrg <bundabrg@grieve.com.au>
Date:   Thu Feb 11 10:31:30 2021 +0800

    Fix buckets for Education

    When interacticing holding an item we will check if the client interacts with both a block and air. If only 1, we send through the block and block_interact packet, otherwise we suppress the block_interact packet and send the other packets. Solves buckets and opening chests whilst holding a bucket.
  • Loading branch information
bundabrg committed Feb 11, 2021
1 parent 6b7c7a0 commit dc8123f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Reversion Developers
* Copyright (c) 2021 Reversion Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -65,4 +65,11 @@ public ReversionSession(RakNetSession connection, EventLoop eventLoop, BedrockWr
* @return the logindata
*/
public abstract LoginData getLoginData();

/**
* Return the Server
*
* @return server instance
*/
public abstract ReversionServer getServer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import au.com.grieve.reversion.protocol.education.v390.Education_v390;
import au.com.grieve.reversion.translators.v390ee_to_v408be.handlers.CreativeContentHandler_v390ee_to_v408be;
import au.com.grieve.reversion.translators.v390ee_to_v408be.handlers.InventoryTransactionHandler_v390ee_to_v408be;
import au.com.grieve.reversion.translators.v390ee_to_v408be.handlers.PlayerActionHandlerHandler_v390ee_to_v408be;
import com.nukkitx.protocol.bedrock.packet.*;

public class Register_v390ee_to_v408be {
Expand Down Expand Up @@ -85,6 +86,7 @@ public class Register_v390ee_to_v408be {
.registerPacketHandler(LoginPacket.class, LoginHandler_Bedrock.class)
.registerPacketHandler(MobArmorEquipmentPacket.class, MobArmorEquipmentHandler_Bedrock.class)
.registerPacketHandler(MobEquipmentPacket.class, MobEquipmentHandler_Bedrock.class)
.registerPacketHandler(PlayerActionPacket.class, PlayerActionHandlerHandler_v390ee_to_v408be.class)
.registerPacketHandler(SetEntityDataPacket.class, SetEntityDataHandler_Bedrock.class)
.registerPacketHandler(StartGamePacket.class, StartGameHandler_Education.class)
.registerPacketHandler(UpdateBlockPacket.class, UpdateBlockHandler_Bedrock.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Reversion Developers
* Copyright (c) 2021 Reversion Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,13 +28,23 @@
import au.com.grieve.reversion.editions.bedrock.handlers.InventoryTransactionHandler_Bedrock;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerId;
import com.nukkitx.protocol.bedrock.data.inventory.InventoryActionData;
import com.nukkitx.protocol.bedrock.data.inventory.TransactionType;
import com.nukkitx.protocol.bedrock.packet.InventoryTransactionPacket;
import com.nukkitx.protocol.bedrock.packet.PlayerActionPacket;
import io.netty.util.concurrent.ScheduledFuture;
import lombok.Data;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class InventoryTransactionHandler_v390ee_to_v408be extends InventoryTransactionHandler_Bedrock {
private InventoryActionData cachedAction;

@Getter
private List<PendingUseItem> pendingUseItems = new ArrayList<>();

public InventoryTransactionHandler_v390ee_to_v408be(BedrockTranslator translator) {
super(translator);
}
Expand Down Expand Up @@ -64,6 +74,60 @@ public boolean fromUpstream(InventoryTransactionPacket packet) {
}
}

// If using an Item with action on a block we will wait up to 5ms to see if we also have an action on the air
// If so, we will send both packets upstream. If not, we send this packet + a block_interact upstream
if (packet.getTransactionType() == TransactionType.ITEM_USE) {
// Action on the block
if (packet.getActionType() == 0) {
PendingUseItem pending = new PendingUseItem();
pending.setFirstPacket(packet);
pending.setScheduledFuture(getTranslator().getReversionSession().getServer().getEventLoopGroup().schedule(() -> {
if (getTranslator().getDownstreamTranslator() != null) {

getTranslator().getDownstreamTranslator().fromUpstream(pending.getFirstPacket());
if (pending.getActionPacket() != null) {
getTranslator().getDownstreamTranslator().fromUpstream(pending.getActionPacket());
}
} else {
getTranslator().toServer(pending.getFirstPacket());
if (pending.getActionPacket() != null) {
getTranslator().toServer(pending.getActionPacket());
}
}
pendingUseItems.remove(pending);
}, 5, TimeUnit.MILLISECONDS));

pendingUseItems.add(pending);

return true;
}

// Action on the air
if (packet.getActionType() == 1) {
PendingUseItem pending = getPendingUseItems().stream()
.filter(i -> i.getFirstPacket().getPlayerPosition().equals(packet.getPlayerPosition()))
.findFirst()
.orElse(null);

if (pending != null) {
pendingUseItems.remove(pending);
pending.getScheduledFuture().cancel(false);
if (getTranslator().getDownstreamTranslator() != null) {
getTranslator().getDownstreamTranslator().fromUpstream(pending.getFirstPacket());
} else {
getTranslator().toServer(pending.getFirstPacket());
}
}
}
}

return false;
}

@Data
public static class PendingUseItem {
private InventoryTransactionPacket firstPacket;
private PlayerActionPacket actionPacket;
private ScheduledFuture<?> scheduledFuture;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* MIT License
*
* Copyright (c) 2021 Reversion Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package au.com.grieve.reversion.translators.v390ee_to_v408be.handlers;

import au.com.grieve.reversion.api.PacketHandler;
import au.com.grieve.reversion.editions.bedrock.BedrockTranslator;
import com.nukkitx.protocol.bedrock.packet.InventoryTransactionPacket;
import com.nukkitx.protocol.bedrock.packet.PlayerActionPacket;

public class PlayerActionHandlerHandler_v390ee_to_v408be extends PacketHandler<BedrockTranslator, PlayerActionPacket> {
public PlayerActionHandlerHandler_v390ee_to_v408be(BedrockTranslator translator) {
super(translator);
}

@Override
public boolean fromUpstream(PlayerActionPacket packet) {
super.fromUpstream(packet);

InventoryTransactionHandler_v390ee_to_v408be ith = (InventoryTransactionHandler_v390ee_to_v408be) getTranslator().getHandlers().get(InventoryTransactionPacket.class);

// If ith has a penderItemUse with the same location as this we will handle it ourself
InventoryTransactionHandler_v390ee_to_v408be.PendingUseItem pending = ith.getPendingUseItems().stream()
.filter(i -> i.getFirstPacket().getBlockPosition().equals(packet.getBlockPosition()))
.findFirst()
.orElse(null);

if (pending != null) {
pending.setActionPacket(packet);
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import au.com.grieve.reversion.protocol.education.v391.Education_v391;
import au.com.grieve.reversion.translators.v390ee_to_v408be.handlers.CreativeContentHandler_v390ee_to_v408be;
import au.com.grieve.reversion.translators.v390ee_to_v408be.handlers.InventoryTransactionHandler_v390ee_to_v408be;
import au.com.grieve.reversion.translators.v390ee_to_v408be.handlers.PlayerActionHandlerHandler_v390ee_to_v408be;
import com.nukkitx.protocol.bedrock.packet.*;

public class Register_v391ee_to_v408be {
Expand Down Expand Up @@ -85,6 +86,7 @@ public class Register_v391ee_to_v408be {
.registerPacketHandler(LoginPacket.class, LoginHandler_Bedrock.class)
.registerPacketHandler(MobArmorEquipmentPacket.class, MobArmorEquipmentHandler_Bedrock.class)
.registerPacketHandler(MobEquipmentPacket.class, MobEquipmentHandler_Bedrock.class)
.registerPacketHandler(PlayerActionPacket.class, PlayerActionHandlerHandler_v390ee_to_v408be.class)
.registerPacketHandler(SetEntityDataPacket.class, SetEntityDataHandler_Bedrock.class)
.registerPacketHandler(StartGamePacket.class, StartGameHandler_Education.class)
.registerPacketHandler(UpdateBlockPacket.class, UpdateBlockHandler_Bedrock.class)
Expand Down

0 comments on commit dc8123f

Please sign in to comment.