Created
December 31, 2020 17:57
-
-
Save cerus/c1a39fbce22bc0b7675a62a13952e958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The NmsMethods interface was written by me, it's not an interface from Bukkit or Minecraft | |
// All of the overridden methods were copied from the CraftBlock class and modified to use the chunk cache | |
public class NmsMethodsImpl implements NmsMethods { | |
private final ChunkCacheImpl chunkCache; | |
public NmsMethodsImpl(final ChunkCacheImpl chunkCache) { | |
this.chunkCache = chunkCache; | |
} | |
@Override | |
public Material getCachedType(final Location location) { | |
final int chunkX = location.getBlockX() >> 4; | |
final int chunkZ = location.getBlockZ() >> 4; | |
final Chunk cachedChunk = this.chunkCache.getCachedChunk(chunkX, chunkZ); | |
return CraftMagicNumbers.getMaterial(cachedChunk.getType(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ())).getBlock()); | |
} | |
@Override | |
public BlockState getCachedState(final Location location) { | |
final Material material = this.getCachedType(location); | |
final CraftBlock block = (CraftBlock) location.getBlock(); | |
switch (material) { | |
case ACACIA_SIGN: | |
case ACACIA_WALL_SIGN: | |
case BIRCH_SIGN: | |
case BIRCH_WALL_SIGN: | |
case CRIMSON_SIGN: | |
case CRIMSON_WALL_SIGN: | |
case DARK_OAK_SIGN: | |
case DARK_OAK_WALL_SIGN: | |
case JUNGLE_SIGN: | |
case JUNGLE_WALL_SIGN: | |
case OAK_SIGN: | |
case OAK_WALL_SIGN: | |
case SPRUCE_SIGN: | |
case SPRUCE_WALL_SIGN: | |
case WARPED_SIGN: | |
case WARPED_WALL_SIGN: | |
return new CraftSign(block); | |
case CHEST: | |
case TRAPPED_CHEST: | |
return new CraftChest(block); | |
case FURNACE: | |
return new CraftFurnaceFurnace(block); | |
case DISPENSER: | |
return new CraftDispenser(block); | |
case DROPPER: | |
return new CraftDropper(block); | |
case END_GATEWAY: | |
return new CraftEndGateway(block); | |
case HOPPER: | |
return new CraftHopper(block); | |
case SPAWNER: | |
return new CraftCreatureSpawner(block); | |
case JUKEBOX: | |
return new CraftJukebox(block); | |
case BREWING_STAND: | |
return new CraftBrewingStand(block); | |
case CREEPER_HEAD: | |
case CREEPER_WALL_HEAD: | |
case DRAGON_HEAD: | |
case DRAGON_WALL_HEAD: | |
case PLAYER_HEAD: | |
case PLAYER_WALL_HEAD: | |
case SKELETON_SKULL: | |
case SKELETON_WALL_SKULL: | |
case WITHER_SKELETON_SKULL: | |
case WITHER_SKELETON_WALL_SKULL: | |
case ZOMBIE_HEAD: | |
case ZOMBIE_WALL_HEAD: | |
return new CraftSkull(block); | |
case COMMAND_BLOCK: | |
case CHAIN_COMMAND_BLOCK: | |
case REPEATING_COMMAND_BLOCK: | |
return new CraftCommandBlock(block); | |
case BEACON: | |
return new CraftBeacon(block); | |
case BLACK_BANNER: | |
case BLACK_WALL_BANNER: | |
case BLUE_BANNER: | |
case BLUE_WALL_BANNER: | |
case BROWN_BANNER: | |
case BROWN_WALL_BANNER: | |
case CYAN_BANNER: | |
case CYAN_WALL_BANNER: | |
case GRAY_BANNER: | |
case GRAY_WALL_BANNER: | |
case GREEN_BANNER: | |
case GREEN_WALL_BANNER: | |
case LIGHT_BLUE_BANNER: | |
case LIGHT_BLUE_WALL_BANNER: | |
case LIGHT_GRAY_BANNER: | |
case LIGHT_GRAY_WALL_BANNER: | |
case LIME_BANNER: | |
case LIME_WALL_BANNER: | |
case MAGENTA_BANNER: | |
case MAGENTA_WALL_BANNER: | |
case ORANGE_BANNER: | |
case ORANGE_WALL_BANNER: | |
case PINK_BANNER: | |
case PINK_WALL_BANNER: | |
case PURPLE_BANNER: | |
case PURPLE_WALL_BANNER: | |
case RED_BANNER: | |
case RED_WALL_BANNER: | |
case WHITE_BANNER: | |
case WHITE_WALL_BANNER: | |
case YELLOW_BANNER: | |
case YELLOW_WALL_BANNER: | |
return new CraftBanner(block); | |
case STRUCTURE_BLOCK: | |
return new CraftStructureBlock(block); | |
case SHULKER_BOX: | |
case WHITE_SHULKER_BOX: | |
case ORANGE_SHULKER_BOX: | |
case MAGENTA_SHULKER_BOX: | |
case LIGHT_BLUE_SHULKER_BOX: | |
case YELLOW_SHULKER_BOX: | |
case LIME_SHULKER_BOX: | |
case PINK_SHULKER_BOX: | |
case GRAY_SHULKER_BOX: | |
case LIGHT_GRAY_SHULKER_BOX: | |
case CYAN_SHULKER_BOX: | |
case PURPLE_SHULKER_BOX: | |
case BLUE_SHULKER_BOX: | |
case BROWN_SHULKER_BOX: | |
case GREEN_SHULKER_BOX: | |
case RED_SHULKER_BOX: | |
case BLACK_SHULKER_BOX: | |
return new CraftShulkerBox(block); | |
case ENCHANTING_TABLE: | |
return new CraftEnchantingTable(block); | |
case ENDER_CHEST: | |
return new CraftEnderChest(block); | |
case DAYLIGHT_DETECTOR: | |
return new CraftDaylightDetector(block); | |
case COMPARATOR: | |
return new CraftComparator(block); | |
case BLACK_BED: | |
case BLUE_BED: | |
case BROWN_BED: | |
case CYAN_BED: | |
case GRAY_BED: | |
case GREEN_BED: | |
case LIGHT_BLUE_BED: | |
case LIGHT_GRAY_BED: | |
case LIME_BED: | |
case MAGENTA_BED: | |
case ORANGE_BED: | |
case PINK_BED: | |
case PURPLE_BED: | |
case RED_BED: | |
case WHITE_BED: | |
case YELLOW_BED: | |
return new CraftBed(block); | |
case CONDUIT: | |
return new CraftConduit(block); | |
case BARREL: | |
return new CraftBarrel(block); | |
case BELL: | |
return new CraftBell(block); | |
case BLAST_FURNACE: | |
return new CraftBlastFurnace(block); | |
case CAMPFIRE: | |
case SOUL_CAMPFIRE: | |
return new CraftCampfire(block); | |
case JIGSAW: | |
return new CraftJigsaw(block); | |
case LECTERN: | |
return new CraftLectern(block); | |
case SMOKER: | |
return new CraftSmoker(block); | |
case BEEHIVE: | |
case BEE_NEST: | |
return new CraftBeehive(block); | |
default: | |
final TileEntity tileEntity = this.chunkCache.getCachedChunk(location.getBlockX() >> 4, location.getBlockZ() >> 4) | |
.getTileEntity(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ())); | |
if (tileEntity != null) { | |
// block with unhandled TileEntity: | |
return new CraftBlockEntityState<>(block, (Class<TileEntity>) tileEntity.getClass()); | |
} else { | |
// Block without TileEntity: | |
return new CraftBlockState(block); | |
} | |
} | |
} | |
@Override | |
public BlockData getCachedData(final Location location) { | |
final int chunkX = location.getBlockX() >> 4; | |
final int chunkZ = location.getBlockZ() >> 4; | |
final Chunk cachedChunk = this.chunkCache.getCachedChunk(chunkX, chunkZ); | |
return CraftBlockData.fromData(cachedChunk.getType(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment