Moved break time checks to BlockUtils class, Created ItemUtils and added helper function to get enchantment levels on items, implemented a entity effect cache, added haste and mining fatigue check for block breaking animations, fixed block breaking animations for blocks with speeds that are not affected by tools.

This commit is contained in:
William Johnstone 2020-03-22 22:59:34 +00:00
parent 4b40b07bf8
commit 60662ae06c
9 changed files with 253 additions and 104 deletions

View File

@ -60,6 +60,12 @@
<version>8.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.nukkitx.fastutil</groupId>
<artifactId>fastutil-object-int-maps</artifactId>
<version>8.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.steveice10</groupId>
<artifactId>opennbt</artifactId>

View File

@ -39,6 +39,7 @@ import lombok.Setter;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.session.cache.EntityEffectCache;
import org.geysermc.connector.utils.SkinUtils;
import java.util.UUID;
@ -50,6 +51,7 @@ public class PlayerEntity extends LivingEntity {
private String username;
private long lastSkinUpdate = -1;
private boolean playerList = true;
private final EntityEffectCache effectCache;
public PlayerEntity(GameProfile gameProfile, long entityId, long geyserId, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, EntityType.PLAYER, position, motion, rotation);
@ -57,6 +59,7 @@ public class PlayerEntity extends LivingEntity {
profile = gameProfile;
uuid = gameProfile.getId();
username = gameProfile.getName();
effectCache = new EntityEffectCache();
if (geyserId == 1) valid = true;
}

View File

@ -395,7 +395,7 @@ public class GeyserSession implements CommandSender {
startGamePacket.setBonusChestEnabled(false);
startGamePacket.setStartingWithMap(false);
startGamePacket.setTrustingPlayers(true);
startGamePacket.setDefaultPlayerPermission(PlayerPermission.OPERATOR);
startGamePacket.setDefaultPlayerPermission(PlayerPermission.MEMBER);
startGamePacket.setServerChunkTickRange(4);
startGamePacket.setBehaviorPackLocked(false);
startGamePacket.setResourcePackLocked(false);

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.session.cache;
import com.github.steveice10.mc.protocol.data.game.entity.Effect;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.Getter;
public class EntityEffectCache {
@Getter
private final Object2IntMap<Effect> entityEffects = new Object2IntOpenHashMap<>();
public void addEffect(Effect effect, int effectAmplifier) {
if (effect != null) {
entityEffects.putIfAbsent(effect, effectAmplifier + 1);
}
}
public void removeEffect(Effect effect) {
if (entityEffects.containsKey(effect)) {
int effectLevel = entityEffects.getInt(effect);
entityEffects.remove(effect, effectLevel);
}
}
public int getEffectLevel(Effect effect) {
return entityEffects.getOrDefault(effect, 0);
}
}

View File

@ -28,6 +28,7 @@ package org.geysermc.connector.network.translators.java.entity;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityEffectPacket;
import com.nukkitx.protocol.bedrock.packet.MobEffectPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.PlayerEntity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.utils.EntityUtils;
@ -39,6 +40,7 @@ public class JavaEntityEffectTranslator extends PacketTranslator<ServerEntityEff
Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) {
entity = session.getPlayerEntity();
((PlayerEntity) entity).getEffectCache().addEffect(packet.getEffect(), packet.getAmplifier());
}
if (entity == null)
return;

View File

@ -28,6 +28,7 @@ package org.geysermc.connector.network.translators.java.entity;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityRemoveEffectPacket;
import com.nukkitx.protocol.bedrock.packet.MobEffectPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.PlayerEntity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.utils.EntityUtils;
@ -39,6 +40,7 @@ public class JavaEntityRemoveEffectTranslator extends PacketTranslator<ServerEnt
Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) {
entity = session.getPlayerEntity();
((PlayerEntity) entity).getEffectCache().removeEffect(packet.getEffect());
}
if (entity == null)
return;

View File

@ -27,6 +27,7 @@ package org.geysermc.connector.network.translators.java.entity.player;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerActionAckPacket;
import com.github.steveice10.opennbt.tag.builtin.*;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.LevelEventType;
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
@ -36,7 +37,7 @@ import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockTranslator;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ToolItemEntry;
import org.geysermc.connector.utils.BlockUtils;
import org.geysermc.connector.utils.ChunkUtils;
public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayerActionAckPacket> {
@ -57,16 +58,16 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
packet.getPosition().getZ()
));
double blockHardness = BlockTranslator.JAVA_RUNTIME_ID_TO_HARDNESS.get(packet.getNewState().getId());
PlayerInventory inventory = session.getInventory();
ItemStack item = inventory.getItemInHand();
System.out.println("item.getNbt() = " + item.getNbt());
ItemEntry itemEntry = null;
CompoundTag nbtData = new CompoundTag("");
if (item != null) {
itemEntry = TranslatorsInit.getItemTranslator().getItem(item);
nbtData = item.getNbt();
System.out.println("item.getNbt() = " + item.getNbt());
}
double breakTime = Math.ceil(getBreakTime(blockHardness, packet.getNewState().getId(), itemEntry) * 20);
double breakTime = Math.ceil(BlockUtils.getBreakTime(blockHardness, packet.getNewState().getId(), itemEntry, nbtData, session.getPlayerEntity()) * 20);
System.out.println("breakTime = " + breakTime);
int data = (int) (65535 / breakTime);
System.out.println("data = " + data);
@ -89,104 +90,6 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
}
}
}
/*private static double speedBonusByEfficiencyLore0(int efficiencyLoreLevel) {
if (efficiencyLoreLevel == 0) return 0;
return efficiencyLoreLevel * efficiencyLoreLevel + 1;
}*/
/*private static double speedRateByHasteLore0(int hasteLoreLevel) {
return 1.0 + (0.2 * hasteLoreLevel);
}*/
private boolean correctTool(String blockToolType, String itemToolType) {
return (blockToolType.equals("sword") && itemToolType.equals("sword")) ||
(blockToolType.equals("shovel") && itemToolType.equals("shovel")) ||
(blockToolType.equals("pickaxe") && itemToolType.equals("pickaxe")) ||
(blockToolType.equals("axe") && itemToolType.equals("axe")) ||
(blockToolType.equals("shears") && itemToolType.equals("shears")) ||
blockToolType.equals("");
}
private double toolBreakTimeBonus(String toolType, String toolTier, boolean isWoolBlock) {
if (toolType.equals("shears")) return isWoolBlock ? 5.0 : 15.0;
if (toolType.equals("")) return 1.0;
switch (toolTier) {
case "wooden":
return 2.0;
case "stone":
return 4.0;
case "iron":
return 6.0;
case "diamond":
return 8.0;
case "golden":
return 12.0;
default:
return 1.0;
}
}
//http://minecraft.gamepedia.com/Breaking
private double calculateBreakTime(double blockHardness, String toolTier, boolean canHarvestWithHand, boolean correctTool,
String toolType, boolean isWoolBlock, boolean isCobweb
/*int efficiencyLoreLevel, int hasteEffectLevel,
boolean insideOfWaterWithoutAquaAffinity, boolean outOfWaterButNotOnGround*/) {
System.out.println("blockHardness = " + blockHardness);
double baseTime = ((correctTool || canHarvestWithHand) ? 1.5 : 5.0) * blockHardness;
System.out.println("baseTime = " + baseTime);
double speed = 1.0 / baseTime;
System.out.println("speed = " + speed);
if (correctTool) {
speed *= toolBreakTimeBonus(toolType, toolTier, isWoolBlock);
} else if (toolType.equals("sword")) {
speed*= (isCobweb ? 15.0 : 1.5);
}
System.out.println("speed = " + speed);
// TODO implement this math
//speed += speedBonusByEfficiencyLore0(efficiencyLoreLevel);
//speed *= speedRateByHasteLore0(hasteEffectLevel);
//if (insideOfWaterWithoutAquaAffinity) speed *= 0.2;
//if (outOfWaterButNotOnGround) speed *= 0.2;
return 1.0 / speed;
}
private double getBreakTime(double blockHardness, int blockId, ItemEntry item) {
boolean isWoolBlock = BlockTranslator.JAVA_RUNTIME_WOOL_IDS.contains(blockId);
boolean isCobweb = blockId == BlockTranslator.JAVA_RUNTIME_COBWEB_ID;
String blockToolType = BlockTranslator.JAVA_RUNTIME_ID_TO_TOOL_TYPE.getOrDefault(blockId, "");
boolean canHarvestWithHand = BlockTranslator.JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND.get(blockId);
String toolType = "";
String toolTier = "";
boolean correctTool = false;
if (item instanceof ToolItemEntry) {
ToolItemEntry toolItem = (ToolItemEntry) item;
toolType = toolItem.getToolType();
toolTier = toolItem.getToolTier();
correctTool = correctTool(blockToolType, toolType);
}
System.out.println("canHarvestWithHand = " + canHarvestWithHand);
System.out.println("correctTool = " + correctTool);
System.out.println("itemToolType = " + toolType);
System.out.println("toolTier = " + toolTier);
System.out.println("isWoolBlock = " + isWoolBlock);
System.out.println("isCobweb = " + isCobweb);
//int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
// .map(Enchantment::getLevel).orElse(0);
//int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
// .map(Effect::getAmplifier).orElse(0);
//boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
// Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
// .map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
//boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
//return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
// efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
double returnValue = calculateBreakTime(blockHardness, toolTier, canHarvestWithHand, correctTool, toolType, isWoolBlock, isCobweb);
System.out.println("returnValue = " + returnValue);
return returnValue;
}
}

View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.utils;
import com.github.steveice10.mc.protocol.data.game.entity.Effect;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.geysermc.connector.entity.PlayerEntity;
import org.geysermc.connector.network.translators.block.BlockTranslator;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ToolItemEntry;
public class BlockUtils {
private static boolean correctTool(String blockToolType, String itemToolType) {
return (blockToolType.equals("sword") && itemToolType.equals("sword")) ||
(blockToolType.equals("shovel") && itemToolType.equals("shovel")) ||
(blockToolType.equals("pickaxe") && itemToolType.equals("pickaxe")) ||
(blockToolType.equals("axe") && itemToolType.equals("axe")) ||
(blockToolType.equals("shears") && itemToolType.equals("shears"));
}
private static double toolBreakTimeBonus(String toolType, String toolTier, boolean isWoolBlock) {
if (toolType.equals("shears")) return isWoolBlock ? 5.0 : 15.0;
if (toolType.equals("")) return 1.0;
switch (toolTier) {
case "wooden":
return 2.0;
case "stone":
return 4.0;
case "iron":
return 6.0;
case "diamond":
return 8.0;
case "golden":
return 12.0;
default:
return 1.0;
}
}
//http://minecraft.gamepedia.com/Breaking
private static double calculateBreakTime(double blockHardness, String toolTier, boolean canHarvestWithHand, boolean correctTool,
String toolType, boolean isWoolBlock, boolean isCobweb, int toolEfficiencyLevel, int hasteLevel, int miningFatigueLevel
/*boolean insideOfWaterWithoutAquaAffinity, boolean outOfWaterButNotOnGround*/) {
System.out.println("blockHardness = " + blockHardness);
double baseTime = ((correctTool || canHarvestWithHand) ? 1.5 : 5.0) * blockHardness;
System.out.println("baseTime = " + baseTime);
double speed = 1.0 / baseTime;
System.out.println("speed = " + speed);
if (correctTool) {
speed *= toolBreakTimeBonus(toolType, toolTier, isWoolBlock);
speed += toolEfficiencyLevel == 0 ? 0 : toolEfficiencyLevel * toolEfficiencyLevel + 1;
} else if (toolType.equals("sword")) {
speed*= (isCobweb ? 15.0 : 1.5);
}
System.out.println("speed before haste = " + speed);
speed *= 1.0 + (0.2 * hasteLevel);
System.out.println("speed = " + speed);
switch (miningFatigueLevel) {
case 0:
break;
case 1:
speed -= (speed * 0.7);
break;
case 2:
speed -= (speed * 0.91);
break;
case 3:
speed -= (speed * 0.9973);
break;
default:
speed -= (speed * 0.99919);
break;
}
System.out.println("miningFatigueLevel = " + miningFatigueLevel);
//if (insideOfWaterWithoutAquaAffinity) speed *= 0.2;
//if (outOfWaterButNotOnGround) speed *= 0.2;
return 1.0 / speed;
}
public static double getBreakTime(double blockHardness, int blockId, ItemEntry item, CompoundTag nbtData, PlayerEntity player) {
boolean isWoolBlock = BlockTranslator.JAVA_RUNTIME_WOOL_IDS.contains(blockId);
boolean isCobweb = blockId == BlockTranslator.JAVA_RUNTIME_COBWEB_ID;
String blockToolType = BlockTranslator.JAVA_RUNTIME_ID_TO_TOOL_TYPE.getOrDefault(blockId, "");
boolean canHarvestWithHand = BlockTranslator.JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND.get(blockId);
String toolType = "";
String toolTier = "";
boolean correctTool = false;
if (item instanceof ToolItemEntry) {
ToolItemEntry toolItem = (ToolItemEntry) item;
toolType = toolItem.getToolType();
toolTier = toolItem.getToolTier();
correctTool = correctTool(blockToolType, toolType);
}
int toolEfficiencyLevel = ItemUtils.getEnchantmentLevel(nbtData, "minecraft:efficiency");
int hasteLevel = player.getEffectCache().getEffectLevel(Effect.FASTER_DIG);
int miningFatigueLevel = player.getEffectCache().getEffectLevel(Effect.SLOWER_DIG);
// TODO implement these checks and material check if possible
//boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
// Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
// .map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
//boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
return calculateBreakTime(blockHardness, toolTier, canHarvestWithHand, correctTool, toolType, isWoolBlock, isCobweb, toolEfficiencyLevel, hasteLevel, miningFatigueLevel);
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.utils;
import com.github.steveice10.opennbt.tag.builtin.*;
public class ItemUtils {
public static int getEnchantmentLevel(CompoundTag itemNBTData, String enchantmentId) {
ListTag enchantments = (itemNBTData == null ? null : itemNBTData.get("Enchantments"));
if (enchantments != null) {
int enchantmentLevel = 0;
for (Tag tag : enchantments) {
CompoundTag enchantment = (CompoundTag) tag;
StringTag enchantId = enchantment.get("id");
if (enchantId.getValue().equals(enchantmentId)) {
enchantmentLevel = (int) ((ShortTag) enchantment.get("lvl")).getValue();
}
}
return enchantmentLevel;
}
return 0;
}
}