Skip to content

Commit

Permalink
BlockTranslator Events
Browse files Browse the repository at this point in the history
  • Loading branch information
bundabrg committed Jul 7, 2020
1 parent 7acf67d commit f380091
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.event.events;

import com.fasterxml.jackson.databind.JsonNode;
import com.nukkitx.nbt.NbtMap;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
public class BuildBedrockStateEvent extends GeyserEvent {
JsonNode blockNode;
NbtMap blockState;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.event.events;

import com.nukkitx.nbt.NbtMap;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
public class BuildBlockStateMapEvent extends CancellableGeyserEvent {
Map<NbtMap, NbtMap> blockStateMap;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.event.EventManager;
import org.geysermc.connector.event.events.BlockEntityRegistryEvent;
import org.geysermc.connector.event.events.BuildBedrockStateEvent;
import org.geysermc.connector.event.events.BuildBlockStateMapEvent;
import org.geysermc.connector.event.events.RuntimeBlockStateReadEvent;
import org.geysermc.connector.network.translators.world.block.entity.BlockEntity;
import org.geysermc.connector.utils.FileUtils;
Expand Down Expand Up @@ -80,26 +82,29 @@ public class BlockTranslator {
private static final int BLOCK_STATE_VERSION = 17825806;

static {
/* Load block palette */
InputStream stream = FileUtils.getResource("bedrock/runtime_block_states.dat");

NbtList<NbtMap> blocksTag;
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
blocksTag = EventManager.getInstance().triggerEvent(new RuntimeBlockStateReadEvent(
(NbtList<NbtMap>) nbtInputStream.readTag())).getEvent().getBlockStates();
} catch (Exception e) {
throw new AssertionError("Unable to get blocks from runtime block states", e);
}

Map<NbtMap, NbtMap> blockStateMap = new HashMap<>();

for (NbtMap tag : blocksTag) {
if (blockStateMap.putIfAbsent(tag.getCompound("block"), tag) != null) {
throw new AssertionError("Duplicate block states in Bedrock palette");
}
}

stream = FileUtils.getResource("mappings/blocks.json");
EventManager.getInstance().triggerEvent(new BuildBlockStateMapEvent(blockStateMap))
.onNotCancelled(result -> {
/* Load block palette */
InputStream stream = FileUtils.getResource("bedrock/runtime_block_states.dat");

NbtList<NbtMap> blocksTag;
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
blocksTag = EventManager.getInstance().triggerEvent(new RuntimeBlockStateReadEvent(
(NbtList<NbtMap>) nbtInputStream.readTag())).getEvent().getBlockStates();
} catch (Exception e) {
throw new AssertionError("Unable to get blocks from runtime block states", e);
}

for (NbtMap tag : blocksTag) {
if (blockStateMap.putIfAbsent(tag.getCompound("block"), tag) != null) {
throw new AssertionError("Duplicate block states in Bedrock palette");
}
}
});

InputStream stream = FileUtils.getResource("mappings/blocks.json");
JsonNode blocks;
try {
blocks = GeyserConnector.JSON_MAPPER.readTree(stream);
Expand Down Expand Up @@ -255,6 +260,7 @@ public class BlockTranslator {
}

BLOCKS = new NbtList<>(NbtType.COMPOUND, paletteList);
System.err.println(BLOCKS);
}

private BlockTranslator() {
Expand Down Expand Up @@ -291,7 +297,8 @@ private static NbtMap buildBedrockState(JsonNode node) {
}
}
tagBuilder.put("states", statesBuilder.build());
return tagBuilder.build();

return EventManager.getInstance().triggerEvent(new BuildBedrockStateEvent(node, tagBuilder.build())).getEvent().getBlockState();
}

public static int getBedrockBlockId(int state) {
Expand Down

0 comments on commit f380091

Please sign in to comment.