PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeAdapter.java

http://github.com/sk89q/worldedit
Java | 250 lines | 184 code | 27 blank | 39 comment | 20 complexity | 39a174647f0451dfdd822ded293d22d8 MD5 | raw file
  1. /*
  2. * WorldEdit, a Minecraft world manipulation toolkit
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldEdit team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldedit.forge;
  20. import com.sk89q.worldedit.blocks.BaseItemStack;
  21. import com.sk89q.worldedit.forge.internal.ForgeTransmogrifier;
  22. import com.sk89q.worldedit.forge.internal.NBTConverter;
  23. import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
  24. import com.sk89q.worldedit.math.BlockVector3;
  25. import com.sk89q.worldedit.math.Vector3;
  26. import com.sk89q.worldedit.registry.state.Property;
  27. import com.sk89q.worldedit.util.Direction;
  28. import com.sk89q.worldedit.util.concurrency.LazyReference;
  29. import com.sk89q.worldedit.world.World;
  30. import com.sk89q.worldedit.world.biome.BiomeType;
  31. import com.sk89q.worldedit.world.biome.BiomeTypes;
  32. import com.sk89q.worldedit.world.block.BlockState;
  33. import com.sk89q.worldedit.world.block.BlockType;
  34. import com.sk89q.worldedit.world.block.BlockTypes;
  35. import com.sk89q.worldedit.world.item.ItemType;
  36. import com.sk89q.worldedit.world.item.ItemTypes;
  37. import net.minecraft.core.BlockPos;
  38. import net.minecraft.core.Registry;
  39. import net.minecraft.resources.ResourceLocation;
  40. import net.minecraft.server.level.ServerLevel;
  41. import net.minecraft.server.level.ServerPlayer;
  42. import net.minecraft.util.StringRepresentable;
  43. import net.minecraft.world.item.Item;
  44. import net.minecraft.world.item.ItemStack;
  45. import net.minecraft.world.level.biome.Biome;
  46. import net.minecraft.world.level.block.Block;
  47. import net.minecraft.world.level.block.state.properties.DirectionProperty;
  48. import net.minecraft.world.phys.Vec3;
  49. import net.minecraftforge.fmllegacy.server.ServerLifecycleHooks;
  50. import net.minecraftforge.registries.ForgeRegistries;
  51. import java.util.Comparator;
  52. import java.util.Map;
  53. import java.util.Objects;
  54. import java.util.TreeMap;
  55. import javax.annotation.Nullable;
  56. import static com.google.common.base.Preconditions.checkNotNull;
  57. public final class ForgeAdapter {
  58. private ForgeAdapter() {
  59. }
  60. public static World adapt(ServerLevel world) {
  61. return new ForgeWorld(world);
  62. }
  63. /**
  64. * Create a Forge world from a WorldEdit world.
  65. *
  66. * @param world the WorldEdit world
  67. * @return a Forge world
  68. */
  69. public static ServerLevel adapt(World world) {
  70. checkNotNull(world);
  71. if (world instanceof ForgeWorld) {
  72. return ((ForgeWorld) world).getWorld();
  73. } else {
  74. // TODO introduce a better cross-platform world API to match more easily
  75. throw new UnsupportedOperationException("Cannot adapt from a " + world.getClass());
  76. }
  77. }
  78. public static Biome adapt(BiomeType biomeType) {
  79. return ServerLifecycleHooks.getCurrentServer()
  80. .registryAccess()
  81. .registryOrThrow(Registry.BIOME_REGISTRY)
  82. .getOptional(new ResourceLocation(biomeType.getId()))
  83. .orElseThrow(() -> new IllegalStateException("No biome for " + biomeType.getId()));
  84. }
  85. public static BiomeType adapt(Biome biome) {
  86. ResourceLocation id = ServerLifecycleHooks.getCurrentServer()
  87. .registryAccess()
  88. .registryOrThrow(Registry.BIOME_REGISTRY)
  89. .getKey(biome);
  90. Objects.requireNonNull(id, "biome is not registered");
  91. return BiomeTypes.get(id.toString());
  92. }
  93. public static Vector3 adapt(Vec3 vector) {
  94. return Vector3.at(vector.x, vector.y, vector.z);
  95. }
  96. public static BlockVector3 adapt(BlockPos pos) {
  97. return BlockVector3.at(pos.getX(), pos.getY(), pos.getZ());
  98. }
  99. public static Vec3 toVec3(BlockVector3 vector) {
  100. return new Vec3(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
  101. }
  102. public static net.minecraft.core.Direction adapt(Direction face) {
  103. switch (face) {
  104. case NORTH: return net.minecraft.core.Direction.NORTH;
  105. case SOUTH: return net.minecraft.core.Direction.SOUTH;
  106. case WEST: return net.minecraft.core.Direction.WEST;
  107. case EAST: return net.minecraft.core.Direction.EAST;
  108. case DOWN: return net.minecraft.core.Direction.DOWN;
  109. case UP:
  110. default:
  111. return net.minecraft.core.Direction.UP;
  112. }
  113. }
  114. public static Direction adaptEnumFacing(@Nullable net.minecraft.core.Direction face) {
  115. if (face == null) {
  116. return null;
  117. }
  118. switch (face) {
  119. case NORTH: return Direction.NORTH;
  120. case SOUTH: return Direction.SOUTH;
  121. case WEST: return Direction.WEST;
  122. case EAST: return Direction.EAST;
  123. case DOWN: return Direction.DOWN;
  124. case UP:
  125. default:
  126. return Direction.UP;
  127. }
  128. }
  129. public static BlockPos toBlockPos(BlockVector3 vector) {
  130. return new BlockPos(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
  131. }
  132. /**
  133. * Adapts property.
  134. * @deprecated without replacement, use the block adapter methods
  135. */
  136. @Deprecated
  137. public static Property<?> adaptProperty(net.minecraft.world.level.block.state.properties.Property<?> property) {
  138. return ForgeTransmogrifier.transmogToWorldEditProperty(property);
  139. }
  140. /**
  141. * Adapts properties.
  142. * @deprecated without replacement, use the block adapter methods
  143. */
  144. @Deprecated
  145. public static Map<Property<?>, Object> adaptProperties(BlockType block, Map<net.minecraft.world.level.block.state.properties.Property<?>, Comparable<?>> mcProps) {
  146. Map<Property<?>, Object> props = new TreeMap<>(Comparator.comparing(Property::getName));
  147. for (Map.Entry<net.minecraft.world.level.block.state.properties.Property<?>, Comparable<?>> prop : mcProps.entrySet()) {
  148. Object value = prop.getValue();
  149. if (prop.getKey() instanceof DirectionProperty) {
  150. value = adaptEnumFacing((net.minecraft.core.Direction) value);
  151. } else if (prop.getKey() instanceof net.minecraft.world.level.block.state.properties.EnumProperty) {
  152. value = ((StringRepresentable) value).getSerializedName();
  153. }
  154. props.put(block.getProperty(prop.getKey().getName()), value);
  155. }
  156. return props;
  157. }
  158. public static net.minecraft.world.level.block.state.BlockState adapt(BlockState blockState) {
  159. int blockStateId = BlockStateIdAccess.getBlockStateId(blockState);
  160. if (!BlockStateIdAccess.isValidInternalId(blockStateId)) {
  161. return ForgeTransmogrifier.transmogToMinecraft(blockState);
  162. }
  163. return Block.stateById(blockStateId);
  164. }
  165. public static BlockState adapt(net.minecraft.world.level.block.state.BlockState blockState) {
  166. int blockStateId = Block.getId(blockState);
  167. BlockState worldEdit = BlockStateIdAccess.getBlockStateById(blockStateId);
  168. if (worldEdit == null) {
  169. return ForgeTransmogrifier.transmogToWorldEdit(blockState);
  170. }
  171. return worldEdit;
  172. }
  173. public static Block adapt(BlockType blockType) {
  174. return ForgeRegistries.BLOCKS.getValue(new ResourceLocation(blockType.getId()));
  175. }
  176. public static BlockType adapt(Block block) {
  177. return BlockTypes.get(ForgeRegistries.BLOCKS.getKey(block).toString());
  178. }
  179. public static Item adapt(ItemType itemType) {
  180. return ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemType.getId()));
  181. }
  182. public static ItemType adapt(Item item) {
  183. return ItemTypes.get(ForgeRegistries.ITEMS.getKey(item).toString());
  184. }
  185. public static ItemStack adapt(BaseItemStack baseItemStack) {
  186. net.minecraft.nbt.CompoundTag forgeCompound = null;
  187. if (baseItemStack.getNbt() != null) {
  188. forgeCompound = NBTConverter.toNative(baseItemStack.getNbt());
  189. }
  190. final ItemStack itemStack = new ItemStack(adapt(baseItemStack.getType()), baseItemStack.getAmount());
  191. itemStack.setTag(forgeCompound);
  192. return itemStack;
  193. }
  194. public static BaseItemStack adapt(ItemStack itemStack) {
  195. net.minecraft.nbt.CompoundTag tag = itemStack.serializeNBT();
  196. if (tag.getAllKeys().isEmpty()) {
  197. tag = null;
  198. } else {
  199. final net.minecraft.nbt.Tag tagTag = tag.get("tag");
  200. if (tagTag instanceof net.minecraft.nbt.CompoundTag) {
  201. tag = ((net.minecraft.nbt.CompoundTag) tagTag);
  202. } else {
  203. tag = null;
  204. }
  205. }
  206. net.minecraft.nbt.CompoundTag finalTag = tag;
  207. return new BaseItemStack(
  208. adapt(itemStack.getItem()),
  209. finalTag == null ? null : LazyReference.from(() -> NBTConverter.fromNative(finalTag)),
  210. itemStack.getCount()
  211. );
  212. }
  213. /**
  214. * Get the WorldEdit proxy for the given player.
  215. *
  216. * @param player the player
  217. * @return the WorldEdit player
  218. */
  219. public static ForgePlayer adaptPlayer(ServerPlayer player) {
  220. checkNotNull(player);
  221. return new ForgePlayer(player);
  222. }
  223. }