Skip to content

Commit

Permalink
Use switch statements, and change default block usage
Browse files Browse the repository at this point in the history
This is to support future changes of using multiple block translators
  • Loading branch information
Camotoy committed Feb 13, 2021
1 parent ce33ca5 commit ec23e40
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@
*/
public class BlockInventoryHolder extends InventoryHolder {
/**
* The default Bedrock block ID to use as a fake block
* The default Java block ID to translate as a fake block
*/
private final int defaultBedrockBlockId;
private final int defaultJavaBlockState;
private final ContainerType containerType;
private final Set<String> validBlocks;

public BlockInventoryHolder(String javaBlockIdentifier, ContainerType containerType, String... validBlocks) {
int javaBlockState = BlockTranslator.getJavaBlockState(javaBlockIdentifier);
this.defaultBedrockBlockId = BlockTranslator.getBedrockBlockId(javaBlockState);
this.defaultJavaBlockState = BlockTranslator.getJavaBlockState(javaBlockIdentifier);
this.containerType = containerType;
if (validBlocks != null) {
this.validBlocks = Sets.newHashSet(validBlocks);
Expand Down Expand Up @@ -90,7 +89,7 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(defaultBedrockBlockId);
blockPacket.setRuntimeId(BlockTranslator.getBedrockBlockId(defaultJavaBlockState));
blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
session.sendUpstreamPacket(blockPacket);
inventory.setHolderPosition(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ private void sendRenamePacket(GeyserSession session, Inventory inventory, ItemDa

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.ANVIL_INPUT) {
return 0;
}
if (slotInfoData.getContainer() == ContainerSlotType.ANVIL_MATERIAL) {
return 1;
}
if (slotInfoData.getContainer() == ContainerSlotType.ANVIL_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) {
return 2;
switch (slotInfoData.getContainer()) {
case ANVIL_INPUT:
return 0;
case ANVIL_MATERIAL:
return 1;
case ANVIL_RESULT:
case CREATIVE_OUTPUT:
return 2;
}
return super.bedrockSlotToJava(slotInfoData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void updateProperty(GeyserSession session, Inventory inventory, int key,
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.BREWING_INPUT) {
// Ingredient
// TODO: This hasn't worked and then suddenly, it did.
return 3;
}
if (slotInfoData.getContainer() == ContainerSlotType.BREWING_RESULT) {
Expand All @@ -98,14 +97,15 @@ public int javaSlotToBedrock(int slot) {

@Override
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
if (slot == 0 || slot == 1 || slot == 2) {
return new BedrockContainerSlot(ContainerSlotType.BREWING_RESULT, javaSlotToBedrock(slot));
}
if (slot == 3) {
return new BedrockContainerSlot(ContainerSlotType.BREWING_INPUT, 0);
}
if (slot == 4) {
return new BedrockContainerSlot(ContainerSlotType.BREWING_FUEL, 4);
switch (slot) {
case 0:
case 1:
case 2:
return new BedrockContainerSlot(ContainerSlotType.BREWING_RESULT, javaSlotToBedrock(slot));
case 3:
return new BedrockContainerSlot(ContainerSlotType.BREWING_INPUT, 0);
case 4:
return new BedrockContainerSlot(ContainerSlotType.BREWING_INPUT, 0);
}
return super.javaSlotToBedrockContainer(slot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public GrindstoneInventoryTranslator() {

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.GRINDSTONE_INPUT) {
return 0;
}
if (slotInfoData.getContainer() == ContainerSlotType.GRINDSTONE_ADDITIONAL) {
return 1;
}
if (slotInfoData.getContainer() == ContainerSlotType.GRINDSTONE_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) {
return 2;
switch (slotInfoData.getContainer()) {
case GRINDSTONE_INPUT:
return 0;
case GRINDSTONE_ADDITIONAL:
return 1;
case GRINDSTONE_RESULT:
case CREATIVE_OUTPUT:
return 2;
}
return super.bedrockSlotToJava(slotInfoData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,16 @@ public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession se

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.LOOM_INPUT) {
return 0;
}
if (slotInfoData.getContainer() == ContainerSlotType.LOOM_DYE) {
return 1;
}
if (slotInfoData.getContainer() == ContainerSlotType.LOOM_MATERIAL) {
return 2;
}
if (slotInfoData.getContainer() == ContainerSlotType.LOOM_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) {
return 3;
switch (slotInfoData.getContainer()) {
case LOOM_INPUT:
return 0;
case LOOM_DYE:
return 1;
case LOOM_MATERIAL:
return 2;
case LOOM_RESULT:
case CREATIVE_OUTPUT:
return 3;
}
return super.bedrockSlotToJava(slotInfoData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public SmithingInventoryTranslator() {

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.SMITHING_TABLE_INPUT) {
return 0;
}
if (slotInfoData.getContainer() == ContainerSlotType.SMITHING_TABLE_MATERIAL) {
return 1;
}
if (slotInfoData.getContainer() == ContainerSlotType.SMITHING_TABLE_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) {
return 2;
switch (slotInfoData.getContainer()) {
case SMITHING_TABLE_INPUT:
return 0;
case SMITHING_TABLE_MATERIAL:
return 1;
case SMITHING_TABLE_RESULT:
case CREATIVE_OUTPUT:
return 2;
}
return super.bedrockSlotToJava(slotInfoData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession se

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.STONECUTTER_INPUT) {
return 0;
}
if (slotInfoData.getContainer() == ContainerSlotType.STONECUTTER_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) {
return 1;
switch (slotInfoData.getContainer()) {
case STONECUTTER_INPUT:
return 0;
case STONECUTTER_RESULT:
case CREATIVE_OUTPUT:
return 1;
}
return super.bedrockSlotToJava(slotInfoData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@
import org.geysermc.connector.network.translators.world.block.entity.DoubleChestBlockEntityTranslator;

public class DoubleChestInventoryTranslator extends ChestInventoryTranslator {
private final int defaultBedrockBlockId;
private final int defaultJavaBlockState;

public DoubleChestInventoryTranslator(int size) {
super(size, 54);
int javaBlockState = BlockTranslator.getJavaBlockState("minecraft:chest[facing=north,type=single,waterlogged=false]");
this.defaultBedrockBlockId = BlockTranslator.getBedrockBlockId(javaBlockState);
this.defaultJavaBlockState = BlockTranslator.getJavaBlockState("minecraft:chest[facing=north,type=single,waterlogged=false]");
}

@Override
Expand Down Expand Up @@ -84,10 +83,12 @@ public void prepareInventory(GeyserSession session, Inventory inventory) {
Vector3i position = session.getPlayerEntity().getPosition().toInt().add(Vector3i.UP);
Vector3i pairPosition = position.add(Vector3i.UNIT_X);

int bedrockBlockId = BlockTranslator.getBedrockBlockId(defaultJavaBlockState);

UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(defaultBedrockBlockId);
blockPacket.setRuntimeId(bedrockBlockId);
blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
session.sendUpstreamPacket(blockPacket);

Expand All @@ -107,7 +108,7 @@ public void prepareInventory(GeyserSession session, Inventory inventory) {
blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(pairPosition);
blockPacket.setRuntimeId(defaultBedrockBlockId);
blockPacket.setRuntimeId(bedrockBlockId);
blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
session.sendUpstreamPacket(blockPacket);

Expand Down

0 comments on commit ec23e40

Please sign in to comment.