2020-03-22 22:59:34 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-03-22 22:59:34 +00:00
|
|
|
*
|
|
|
|
* 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;
|
2021-03-18 04:53:14 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
2020-03-22 22:59:34 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2020-10-08 22:44:15 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2020-05-23 23:26:20 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-03-22 22:59:34 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ToolItemEntry;
|
2021-03-18 04:53:14 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
2021-05-03 01:47:11 +00:00
|
|
|
import org.geysermc.connector.registry.type.BlockMapping;
|
2020-05-23 23:26:20 +00:00
|
|
|
|
2020-03-22 22:59:34 +00:00
|
|
|
public class BlockUtils {
|
2021-03-18 04:53:14 +00:00
|
|
|
/**
|
|
|
|
* A static constant of {@link Position} with all values being zero.
|
|
|
|
*/
|
|
|
|
public static final Position POSITION_ZERO = new Position(0, 0, 0);
|
2020-03-22 22:59:34 +00:00
|
|
|
|
2021-05-20 02:24:11 +00:00
|
|
|
private static boolean correctTool(GeyserSession session, BlockMapping blockMapping, String itemToolType) {
|
|
|
|
switch (itemToolType) {
|
|
|
|
case "axe":
|
|
|
|
return session.getTagCache().isAxeEffective(blockMapping);
|
|
|
|
case "hoe":
|
|
|
|
return session.getTagCache().isHoeEffective(blockMapping);
|
|
|
|
case "pickaxe":
|
|
|
|
return session.getTagCache().isPickaxeEffective(blockMapping);
|
|
|
|
case "shears":
|
|
|
|
return session.getTagCache().isShearsEffective(blockMapping);
|
|
|
|
case "shovel":
|
|
|
|
return session.getTagCache().isShovelEffective(blockMapping);
|
|
|
|
case "sword":
|
|
|
|
return blockMapping.getJavaBlockId() == BlockTranslator.JAVA_COBWEB_BLOCK_ID;
|
|
|
|
default:
|
|
|
|
session.getConnector().getLogger().warning("Unknown tool type: " + itemToolType);
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-22 22:59:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 02:24:11 +00:00
|
|
|
private static double toolBreakTimeBonus(String toolType, String toolTier, boolean isShearsEffective) {
|
|
|
|
if (toolType.equals("shears")) return isShearsEffective ? 5.0 : 15.0;
|
2020-03-22 22:59:34 +00:00
|
|
|
if (toolType.equals("")) return 1.0;
|
|
|
|
switch (toolTier) {
|
2021-01-08 03:43:36 +00:00
|
|
|
// https://minecraft.gamepedia.com/Breaking#Speed
|
2020-03-22 22:59:34 +00:00
|
|
|
case "wooden":
|
|
|
|
return 2.0;
|
|
|
|
case "stone":
|
|
|
|
return 4.0;
|
|
|
|
case "iron":
|
|
|
|
return 6.0;
|
|
|
|
case "diamond":
|
|
|
|
return 8.0;
|
2021-01-08 03:43:36 +00:00
|
|
|
case "netherite":
|
|
|
|
return 9.0;
|
2020-03-22 22:59:34 +00:00
|
|
|
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,
|
2021-05-20 02:24:11 +00:00
|
|
|
String toolType, boolean isShearsEffective, int toolEfficiencyLevel, int hasteLevel, int miningFatigueLevel,
|
2020-05-23 23:26:20 +00:00
|
|
|
boolean insideOfWaterWithoutAquaAffinity, boolean outOfWaterButNotOnGround, boolean insideWaterAndNotOnGround) {
|
2020-03-22 22:59:34 +00:00
|
|
|
double baseTime = ((correctTool || canHarvestWithHand) ? 1.5 : 5.0) * blockHardness;
|
|
|
|
double speed = 1.0 / baseTime;
|
|
|
|
|
|
|
|
if (correctTool) {
|
2021-05-20 02:24:11 +00:00
|
|
|
speed *= toolBreakTimeBonus(toolType, toolTier, isShearsEffective);
|
2020-03-22 22:59:34 +00:00
|
|
|
speed += toolEfficiencyLevel == 0 ? 0 : toolEfficiencyLevel * toolEfficiencyLevel + 1;
|
|
|
|
}
|
|
|
|
speed *= 1.0 + (0.2 * hasteLevel);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-23 23:26:20 +00:00
|
|
|
if (insideOfWaterWithoutAquaAffinity) speed *= 0.2;
|
|
|
|
if (outOfWaterButNotOnGround) speed *= 0.2;
|
|
|
|
if (insideWaterAndNotOnGround) speed *= 0.2;
|
2020-03-22 22:59:34 +00:00
|
|
|
return 1.0 / speed;
|
|
|
|
}
|
|
|
|
|
2021-05-03 01:47:11 +00:00
|
|
|
public static double getBreakTime(GeyserSession session, BlockMapping blockMapping, ItemEntry item, CompoundTag nbtData, boolean isSessionPlayer) {
|
2021-05-20 02:24:11 +00:00
|
|
|
boolean isShearsEffective = session.getTagCache().isShearsEffective(blockMapping); //TODO called twice
|
2021-05-03 01:47:11 +00:00
|
|
|
boolean canHarvestWithHand = blockMapping.isCanBreakWithHand();
|
2020-03-22 22:59:34 +00:00
|
|
|
String toolType = "";
|
|
|
|
String toolTier = "";
|
|
|
|
boolean correctTool = false;
|
|
|
|
if (item instanceof ToolItemEntry) {
|
|
|
|
ToolItemEntry toolItem = (ToolItemEntry) item;
|
|
|
|
toolType = toolItem.getToolType();
|
|
|
|
toolTier = toolItem.getToolTier();
|
2021-05-20 02:24:11 +00:00
|
|
|
correctTool = correctTool(session, blockMapping, toolType);
|
2020-03-22 22:59:34 +00:00
|
|
|
}
|
|
|
|
int toolEfficiencyLevel = ItemUtils.getEnchantmentLevel(nbtData, "minecraft:efficiency");
|
2020-05-08 05:18:05 +00:00
|
|
|
int hasteLevel = 0;
|
|
|
|
int miningFatigueLevel = 0;
|
2020-03-27 23:39:53 +00:00
|
|
|
|
2021-05-03 01:47:11 +00:00
|
|
|
if (!isSessionPlayer) {
|
|
|
|
// Another entity is currently mining; we have all the information we know
|
2021-05-20 02:24:11 +00:00
|
|
|
return calculateBreakTime(blockMapping.getHardness(), toolTier, canHarvestWithHand, correctTool, toolType, isShearsEffective,
|
|
|
|
toolEfficiencyLevel, hasteLevel, miningFatigueLevel, false,
|
2021-05-03 01:47:11 +00:00
|
|
|
false, false);
|
2020-05-08 05:18:05 +00:00
|
|
|
}
|
2020-05-23 23:26:20 +00:00
|
|
|
|
2020-11-20 19:56:39 +00:00
|
|
|
hasteLevel = session.getEffectCache().getEffectLevel(Effect.FASTER_DIG);
|
|
|
|
miningFatigueLevel = session.getEffectCache().getEffectLevel(Effect.SLOWER_DIG);
|
2020-05-23 23:26:20 +00:00
|
|
|
|
2021-04-12 04:35:53 +00:00
|
|
|
boolean isInWater = session.getCollisionManager().isPlayerInWater();
|
2020-05-23 23:26:20 +00:00
|
|
|
|
|
|
|
boolean insideOfWaterWithoutAquaAffinity = isInWater &&
|
2020-10-16 23:25:05 +00:00
|
|
|
ItemUtils.getEnchantmentLevel(session.getPlayerInventory().getItem(5).getNbt(), "minecraft:aqua_affinity") < 1;
|
2020-05-23 23:26:20 +00:00
|
|
|
|
|
|
|
boolean outOfWaterButNotOnGround = (!isInWater) && (!session.getPlayerEntity().isOnGround());
|
|
|
|
boolean insideWaterNotOnGround = isInWater && !session.getPlayerEntity().isOnGround();
|
2021-05-20 02:24:11 +00:00
|
|
|
return calculateBreakTime(blockMapping.getHardness(), toolTier, canHarvestWithHand, correctTool, toolType, isShearsEffective,
|
|
|
|
toolEfficiencyLevel, hasteLevel, miningFatigueLevel, insideOfWaterWithoutAquaAffinity,
|
2021-05-03 01:47:11 +00:00
|
|
|
outOfWaterButNotOnGround, insideWaterNotOnGround);
|
2020-03-22 22:59:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 22:44:15 +00:00
|
|
|
/**
|
|
|
|
* Given a position, return the position if a block were located on the specified block face.
|
|
|
|
* @param blockPos the block position
|
|
|
|
* @param face the face of the block - see {@link com.github.steveice10.mc.protocol.data.game.world.block.BlockFace}
|
|
|
|
* @return the block position with the block face accounted for
|
|
|
|
*/
|
|
|
|
public static Vector3i getBlockPosition(Vector3i blockPos, int face) {
|
|
|
|
switch (face) {
|
|
|
|
case 0:
|
|
|
|
return blockPos.sub(0, 1, 0);
|
|
|
|
case 1:
|
|
|
|
return blockPos.add(0, 1, 0);
|
|
|
|
case 2:
|
|
|
|
return blockPos.sub(0, 0, 1);
|
|
|
|
case 3:
|
|
|
|
return blockPos.add(0, 0, 1);
|
|
|
|
case 4:
|
|
|
|
return blockPos.sub(1, 0, 0);
|
|
|
|
case 5:
|
|
|
|
return blockPos.add(1, 0, 0);
|
|
|
|
}
|
|
|
|
return blockPos;
|
|
|
|
}
|
|
|
|
|
2020-03-22 22:59:34 +00:00
|
|
|
}
|