Refactor code and improve sword breaking

This commit is contained in:
William Johnstone 2020-03-21 21:37:55 +00:00
parent 09cdcbdf94
commit 4b40b07bf8
5 changed files with 69 additions and 36 deletions

View file

@ -53,7 +53,7 @@ public class BlockTranslator {
private static final Map<BlockState, String> JAVA_ID_TO_BLOCK_ENTITY_MAP = new HashMap<>(); private static final Map<BlockState, String> JAVA_ID_TO_BLOCK_ENTITY_MAP = new HashMap<>();
public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap(); public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap();
public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_BREAK_WITH_HAND = new Int2BooleanOpenHashMap(); public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap();
public static final Int2ObjectMap<String> JAVA_RUNTIME_ID_TO_TOOL_TYPE = new Int2ObjectOpenHashMap<>(); public static final Int2ObjectMap<String> JAVA_RUNTIME_ID_TO_TOOL_TYPE = new Int2ObjectOpenHashMap<>();
// For block breaking animation math // For block breaking animation math
@ -109,7 +109,7 @@ public class BlockTranslator {
JAVA_RUNTIME_ID_TO_HARDNESS.put(javaRuntimeId, hardnessNode.doubleValue()); JAVA_RUNTIME_ID_TO_HARDNESS.put(javaRuntimeId, hardnessNode.doubleValue());
} }
JAVA_RUNTIME_ID_TO_CAN_BREAK_WITH_HAND.put(javaRuntimeId, entry.getValue().get("can_break_with_hand").booleanValue()); JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND.put(javaRuntimeId, entry.getValue().get("can_break_with_hand").booleanValue());
JsonNode toolTypeNode = entry.getValue().get("tool_type"); JsonNode toolTypeNode = entry.getValue().get("tool_type");
if (toolTypeNode != null) { if (toolTypeNode != null) {
@ -120,7 +120,6 @@ public class BlockTranslator {
JAVA_RUNTIME_WOOL_IDS.add(javaRuntimeId); JAVA_RUNTIME_WOOL_IDS.add(javaRuntimeId);
} }
if (javaId.contains("cobweb")) { if (javaId.contains("cobweb")) {
cobwebRuntimeId = javaRuntimeId; cobwebRuntimeId = javaRuntimeId;
} }

View file

@ -32,16 +32,13 @@ import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
public class ItemEntry { public class ItemEntry {
public static ItemEntry AIR = new ItemEntry("minecraft:air", 0, 0, 0, "none", "none"); public static ItemEntry AIR = new ItemEntry("minecraft:air", 0, 0, 0);
private String javaIdentifier; private final String javaIdentifier;
private int javaId; private final int javaId;
private int bedrockId; private final int bedrockId;
private int bedrockData; private final int bedrockData;
private String toolType;
private String toolTier;
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {

View file

@ -0,0 +1,15 @@
package org.geysermc.connector.network.translators.item;
import lombok.Getter;
@Getter
public class ToolItemEntry extends ItemEntry {
private final String toolType;
private final String toolTier;
public ToolItemEntry(String javaIdentifier, int javaId, int bedrockId, int bedrockData, String toolType, String toolTier) {
super(javaIdentifier, javaId, bedrockId, bedrockData);
this.toolType = toolType;
this.toolTier = toolTier;
}
}

View file

@ -36,6 +36,7 @@ import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockTranslator; import org.geysermc.connector.network.translators.block.BlockTranslator;
import org.geysermc.connector.network.translators.item.ItemEntry; import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ToolItemEntry;
import org.geysermc.connector.utils.ChunkUtils; import org.geysermc.connector.utils.ChunkUtils;
public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayerActionAckPacket> { public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayerActionAckPacket> {
@ -59,6 +60,8 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
PlayerInventory inventory = session.getInventory(); PlayerInventory inventory = session.getInventory();
ItemStack item = inventory.getItemInHand(); ItemStack item = inventory.getItemInHand();
System.out.println("item.getNbt() = " + item.getNbt());
ItemEntry itemEntry = null; ItemEntry itemEntry = null;
if (item != null) { if (item != null) {
itemEntry = TranslatorsInit.getItemTranslator().getItem(item); itemEntry = TranslatorsInit.getItemTranslator().getItem(item);
@ -106,10 +109,9 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
blockToolType.equals(""); blockToolType.equals("");
} }
private double toolBreakTimeBonus0(String toolType, String toolTier, boolean isWoolBlock, boolean isCobweb) { private double toolBreakTimeBonus(String toolType, String toolTier, boolean isWoolBlock) {
if (toolType.equals("sword")) return isCobweb ? 15.0 : 1.0;
if (toolType.equals("shears")) return isWoolBlock ? 5.0 : 15.0; if (toolType.equals("shears")) return isWoolBlock ? 5.0 : 15.0;
if (toolType.equals("none")) return 1.0; if (toolType.equals("")) return 1.0;
switch (toolTier) { switch (toolTier) {
case "wooden": case "wooden":
return 2.0; return 2.0;
@ -127,7 +129,7 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
} }
//http://minecraft.gamepedia.com/Breaking //http://minecraft.gamepedia.com/Breaking
private double breakTime0(double blockHardness, String toolTier, boolean canHarvestWithHand, boolean correctTool, private double calculateBreakTime(double blockHardness, String toolTier, boolean canHarvestWithHand, boolean correctTool,
String toolType, boolean isWoolBlock, boolean isCobweb String toolType, boolean isWoolBlock, boolean isCobweb
/*int efficiencyLoreLevel, int hasteEffectLevel, /*int efficiencyLoreLevel, int hasteEffectLevel,
boolean insideOfWaterWithoutAquaAffinity, boolean outOfWaterButNotOnGround*/) { boolean insideOfWaterWithoutAquaAffinity, boolean outOfWaterButNotOnGround*/) {
@ -137,7 +139,11 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
double speed = 1.0 / baseTime; double speed = 1.0 / baseTime;
System.out.println("speed = " + speed); System.out.println("speed = " + speed);
if (correctTool) speed *= toolBreakTimeBonus0(toolType,toolTier, isWoolBlock, isCobweb); if (correctTool) {
speed *= toolBreakTimeBonus(toolType, toolTier, isWoolBlock);
} else if (toolType.equals("sword")) {
speed*= (isCobweb ? 15.0 : 1.5);
}
System.out.println("speed = " + speed); System.out.println("speed = " + speed);
// TODO implement this math // TODO implement this math
//speed += speedBonusByEfficiencyLore0(efficiencyLoreLevel); //speed += speedBonusByEfficiencyLore0(efficiencyLoreLevel);
@ -148,26 +154,25 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
} }
private double getBreakTime(double blockHardness, int blockId, ItemEntry item) { 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, ""); String blockToolType = BlockTranslator.JAVA_RUNTIME_ID_TO_TOOL_TYPE.getOrDefault(blockId, "");
boolean canHarvestWithHand = BlockTranslator.JAVA_RUNTIME_ID_TO_CAN_BREAK_WITH_HAND.get(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("canHarvestWithHand = " + canHarvestWithHand);
String toolTier = "none";
if (item != null) {
toolTier = item.getToolTier();
}
String toolType = "none";
if (item != null) {
toolType = item.getToolType();
}
boolean correctTool = correctTool(blockToolType, toolType);
System.out.println("correctTool = " + correctTool); System.out.println("correctTool = " + correctTool);
System.out.println("itemToolType = " + toolType); System.out.println("itemToolType = " + toolType);
System.out.println("toolTier = " + toolTier); System.out.println("toolTier = " + toolTier);
boolean isWoolBlock = BlockTranslator.JAVA_RUNTIME_WOOL_IDS.contains(blockId);
boolean isCobweb = blockId == BlockTranslator.JAVA_RUNTIME_COBWEB_ID;
System.out.println("isWoolBlock = " + isWoolBlock); System.out.println("isWoolBlock = " + isWoolBlock);
System.out.println("isCobweb = " + isCobweb); System.out.println("isCobweb = " + isCobweb);
//int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY)) //int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
// .map(Enchantment::getLevel).orElse(0); // .map(Enchantment::getLevel).orElse(0);
//int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE)) //int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
@ -178,7 +183,7 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
//boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround()); //boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
//return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier, //return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
// efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround); // efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
double returnValue = breakTime0(blockHardness, toolTier, canHarvestWithHand, correctTool, toolType, isWoolBlock, isCobweb); double returnValue = calculateBreakTime(blockHardness, toolTier, canHarvestWithHand, correctTool, toolType, isWoolBlock, isCobweb);
System.out.println("returnValue = " + returnValue); System.out.println("returnValue = " + returnValue);
return returnValue; return returnValue;
} }

View file

@ -40,6 +40,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.translators.item.ItemEntry; import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ToolItemEntry;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@ -103,12 +104,28 @@ public class Toolbox {
Iterator<Map.Entry<String, JsonNode>> iterator = items.fields(); Iterator<Map.Entry<String, JsonNode>> iterator = items.fields();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<String, JsonNode> entry = iterator.next(); Map.Entry<String, JsonNode> entry = iterator.next();
ITEM_ENTRIES.put(itemIndex, new ItemEntry( if (entry.getValue().has("tool_type")) {
if (entry.getValue().has("tool_tier")) {
ITEM_ENTRIES.put(itemIndex, new ToolItemEntry(
entry.getKey(), itemIndex, entry.getKey(), itemIndex,
entry.getValue().get("bedrock_id").intValue(), entry.getValue().get("bedrock_id").intValue(),
entry.getValue().get("bedrock_data").intValue(), entry.getValue().get("bedrock_data").intValue(),
entry.getValue().get("tool_type").textValue(), entry.getValue().get("tool_type").textValue(),
entry.getValue().get("tool_tier").textValue())); entry.getValue().get("tool_tier").textValue()));
} else {
ITEM_ENTRIES.put(itemIndex, new ToolItemEntry(
entry.getKey(), itemIndex,
entry.getValue().get("bedrock_id").intValue(),
entry.getValue().get("bedrock_data").intValue(),
entry.getValue().get("tool_type").textValue(),
""));
}
} else {
ITEM_ENTRIES.put(itemIndex, new ItemEntry(
entry.getKey(), itemIndex,
entry.getValue().get("bedrock_id").intValue(),
entry.getValue().get("bedrock_data").intValue()));
}
itemIndex++; itemIndex++;
} }