From 29b6c84230ad35a1d1aa60ed573c4141d25efb4d Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Tue, 19 Feb 2019 20:30:52 +1000 Subject: [PATCH] Rebase and properly setup the registries --- .../sk89q/worldedit/forge/ForgeAdapter.java | 58 +++++++++++-- .../worldedit/forge/ForgeBiomeRegistry.java | 1 - .../com/sk89q/worldedit/forge/ForgeWorld.java | 82 ++++--------------- .../sk89q/worldedit/forge/ForgeWorldEdit.java | 42 ++++++---- .../sk89q/worldedit/forge/NBTConverter.java | 4 +- .../worldedit/forge/TileEntityUtils.java | 6 +- 6 files changed, 101 insertions(+), 92 deletions(-) diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeAdapter.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeAdapter.java index ef05edfaa..58aba645c 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeAdapter.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeAdapter.java @@ -31,22 +31,21 @@ import com.sk89q.worldedit.registry.state.IntegerProperty; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.world.World; - import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeTypes; -import net.minecraft.block.properties.IProperty; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.ResourceLocation; +import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.state.DirectionProperty; import net.minecraft.state.IProperty; +import net.minecraft.state.StateContainer; import net.minecraft.util.EnumFacing; import net.minecraft.util.IStringSerializable; import net.minecraft.util.ResourceLocation; @@ -55,6 +54,9 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.biome.Biome; import net.minecraftforge.registries.ForgeRegistries; +import java.util.Comparator; +import java.util.Map; +import java.util.TreeMap; import java.util.stream.Collectors; final class ForgeAdapter { @@ -67,7 +69,7 @@ final class ForgeAdapter { } public static Biome adapt(BiomeType biomeType) { - return Biome.REGISTRY.getObject(new ResourceLocation(biomeType.getId())); + return ForgeRegistries.BIOMES.getValue(new ResourceLocation(biomeType.getId())); } public static BiomeType adapt(Biome biome) { @@ -136,6 +138,52 @@ final class ForgeAdapter { return new IPropertyAdapter<>(property); } + public static Map, Object> adaptProperties(BlockType block, Map, Comparable> mcProps) { + Map, Object> props = new TreeMap<>(Comparator.comparing(Property::getName)); + for (Map.Entry, Comparable> prop : mcProps.entrySet()) { + Object value = prop.getValue(); + if (prop.getKey() instanceof DirectionProperty) { + value = adaptEnumFacing((EnumFacing) value); + } else if (prop.getKey() instanceof net.minecraft.state.EnumProperty) { + value = ((IStringSerializable) value).getName(); + } + props.put(block.getProperty(prop.getKey().getName()), value); + } + return props; + } + + private static IBlockState applyProperties(StateContainer stateContainer, IBlockState newState, Map, Object> states) { + for (Map.Entry, Object> state : states.entrySet()) { + IProperty property = stateContainer.getProperty(state.getKey().getName()); + Comparable value = (Comparable) state.getValue(); + // we may need to adapt this value, depending on the source prop + if (property instanceof DirectionProperty) { + Direction dir = (Direction) value; + value = ForgeAdapter.adapt(dir); + } else if (property instanceof net.minecraft.state.EnumProperty) { + String enumName = (String) value; + value = ((net.minecraft.state.EnumProperty) property).parseValue((String) value).orElseGet(() -> { + throw new IllegalStateException("Enum property " + property.getName() + " does not contain " + enumName); + }); + } + + newState = newState.with(property, value); + } + return newState; + } + + public static IBlockState adapt(BlockState blockState) { + Block mcBlock = ForgeAdapter.adapt(blockState.getBlockType()); + IBlockState newState = mcBlock.getDefaultState(); + Map, Object> states = blockState.getStates(); + return applyProperties(mcBlock.getStateContainer(), newState, states); + } + + public static BlockState adapt(IBlockState blockState) { + BlockType blockType = adapt(blockState.getBlock()); + return blockType.getState(ForgeAdapter.adaptProperties(blockType, blockState.getValues())); + } + public static Block adapt(BlockType blockType) { return ForgeRegistries.BLOCKS.getValue(new ResourceLocation(blockType.getId())); } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java index b38a625ab..e0337d735 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java @@ -23,7 +23,6 @@ import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.registry.BiomeRegistry; import net.minecraft.world.biome.Biome; -import net.minecraftforge.registries.ForgeRegistries; /** * Provides access to biome data in Forge. diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java index 991db00f0..da9d0bf61 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java @@ -36,7 +36,6 @@ import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.TreeGenerator.TreeType; @@ -45,11 +44,9 @@ import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockStateHolder; -import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.weather.WeatherType; import com.sk89q.worldedit.world.weather.WeatherTypes; -import net.minecraft.block.Block; import net.minecraft.block.BlockLeaves; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityType; @@ -61,19 +58,12 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUseContext; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.server.MinecraftServer; -import net.minecraft.state.DirectionProperty; -import net.minecraft.state.EnumProperty; -import net.minecraft.state.IProperty; -import net.minecraft.state.StateContainer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.IStringSerializable; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.WorldServer; -import net.minecraft.world.biome.Biome; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.chunk.storage.AnvilSaveHandler; @@ -100,11 +90,8 @@ import net.minecraftforge.common.DimensionManager; import java.io.File; import java.lang.ref.WeakReference; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; -import java.util.Map; import java.util.Random; -import java.util.TreeMap; import java.util.concurrent.ThreadLocalRandom; import javax.annotation.Nullable; @@ -182,10 +169,7 @@ public class ForgeWorld extends AbstractWorld { Chunk chunk = world.getChunk(x >> 4, z >> 4); BlockPos pos = new BlockPos(x, y, z); IBlockState old = chunk.getBlockState(pos); - Block mcBlock = Block.getBlockFromName(block.getBlockType().getId()); - IBlockState newState = mcBlock.getDefaultState(); - Map, Object> states = block.getStates(); - newState = applyProperties(mcBlock.getStateContainer(), newState, states); + IBlockState newState = ForgeAdapter.adapt(block.toImmutableState()); IBlockState successState = chunk.setBlockState(pos, newState, false); boolean successful = successState != null; @@ -195,7 +179,7 @@ public class ForgeWorld extends AbstractWorld { // Kill the old TileEntity world.removeTileEntity(pos); NBTTagCompound nativeTag = NBTConverter.toNative(((BaseBlock) block).getNbtData()); - nativeTag.setString("id", ((BaseBlock) block).getNbtId()); + nativeTag.putString("id", ((BaseBlock) block).getNbtId()); TileEntityUtils.setTileEntity(world, position, nativeTag); } } @@ -217,26 +201,6 @@ public class ForgeWorld extends AbstractWorld { return false; } - private IBlockState applyProperties(StateContainer stateContainer, IBlockState newState, Map, Object> states) { - for (Map.Entry, Object> state : states.entrySet()) { - IProperty property = stateContainer.getProperty(state.getKey().getName()); - Comparable value = (Comparable) state.getValue(); - // we may need to adapt this value, depending on the source prop - if (property instanceof DirectionProperty) { - Direction dir = (Direction) value; - value = ForgeAdapter.adapt(dir); - } else if (property instanceof EnumProperty) { - String enumName = (String) value; - value = ((EnumProperty) property).parseValue((String) value).orElseGet(() -> { - throw new IllegalStateException("Enum property " + property.getName() + " does not contain " + enumName); - }); - } - - newState = newState.with(property, value); - } - return newState; - } - @Override public int getBlockLightLevel(BlockVector3 position) { checkNotNull(position); @@ -340,8 +304,7 @@ public class ForgeWorld extends AbstractWorld { MinecraftServer server = originalWorld.getServer(); AnvilSaveHandler saveHandler = new AnvilSaveHandler(saveFolder, originalWorld.getSaveHandler().getWorldDirectory().getName(), server, server.getDataFixer()); - World freshWorld = (World) new WorldServer(server, saveHandler, originalWorld.getWorldInfo(), - originalWorld.dimension.getId(), originalWorld.profiler).init(); + World freshWorld = new WorldServer(server, saveHandler, originalWorld.getSavedDataStorage(), originalWorld.getWorldInfo(), originalWorld.dimension.getType(), originalWorld.profiler).func_212251_i__(); // Pre-gen all the chunks // We need to also pull one more chunk in every direction @@ -359,8 +322,8 @@ public class ForgeWorld extends AbstractWorld { throw new RuntimeException(e); } finally { saveFolder.delete(); - DimensionManager.setWorld(originalWorld.dimension.getId(), null, server); - DimensionManager.setWorld(originalWorld.dimension.getId(), originalWorld, server); + DimensionManager.setWorld(originalWorld.dimension.getType(), null, server); + DimensionManager.setWorld(originalWorld.dimension.getType(), originalWorld, server); } return true; @@ -396,7 +359,7 @@ public class ForgeWorld extends AbstractWorld { @Override public boolean generateTree(TreeType type, EditSession editSession, BlockVector3 position) throws MaxChangedBlocksException { Feature generator = createTreeFeatureGenerator(type); - return generator != null && generator.func_212245_a(getWorld(), getWorld().getChunkProvider().getChunkGenerator(), random, ForgeAdapter.toBlockPos(position), new NoFeatureConfig()); + return generator != null && generator.place(getWorld(), getWorld().getChunkProvider().getChunkGenerator(), random, ForgeAdapter.toBlockPos(position), new NoFeatureConfig()); } @Override @@ -455,15 +418,15 @@ public class ForgeWorld extends AbstractWorld { @Override public void setWeather(WeatherType weatherType, long duration) { WorldInfo info = getWorld().getWorldInfo(); - if (WeatherTypes.THUNDER_STORM.equals(weatherType)) { + if (weatherType == WeatherTypes.THUNDER_STORM) { info.setClearWeatherTime(0); info.setThundering(true); info.setThunderTime((int) duration); - } else if (WeatherTypes.RAIN.equals(weatherType)) { + } else if (weatherType == WeatherTypes.RAIN) { info.setClearWeatherTime(0); info.setRaining(true); info.setRainTime((int) duration); - } else if (WeatherTypes.CLEAR.equals(weatherType)) { + } else if (weatherType == WeatherTypes.CLEAR) { info.setRaining(false); info.setThundering(false); info.setClearWeatherTime((int) duration); @@ -477,26 +440,13 @@ public class ForgeWorld extends AbstractWorld { @Override public BlockState getBlock(BlockVector3 position) { - World world = getWorld(); - BlockPos pos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ()); - IBlockState mcState = world.getBlockState(pos); + IBlockState mcState = getWorld().getChunk(position.getBlockX() >> 4, position.getBlockZ() >> 4).getBlockState( + position.getBlockX(), + position.getBlockY(), + position.getBlockZ() + ); - BlockType blockType = ForgeAdapter.adapt(mcState.getBlock()); - return blockType.getState(adaptProperties(blockType, mcState.getValues())); - } - - private Map, Object> adaptProperties(BlockType block, Map, Comparable> mcProps) { - Map, Object> props = new TreeMap<>(Comparator.comparing(Property::getName)); - for (Map.Entry, Comparable> prop : mcProps.entrySet()) { - Object value = prop.getValue(); - if (prop.getKey() instanceof DirectionProperty) { - value = ForgeAdapter.adaptEnumFacing((EnumFacing) value); - } else if (prop.getKey() instanceof EnumProperty) { - value = ((IStringSerializable) value).getName(); - } - props.put(block.getProperty(prop.getKey().getName()), value); - } - return props; + return ForgeAdapter.adapt(mcState); } @Override @@ -562,7 +512,7 @@ public class ForgeWorld extends AbstractWorld { if (nativeTag != null) { NBTTagCompound tag = NBTConverter.toNative(entity.getNbtData()); for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.removeTag(name); + tag.remove(name); } createdEntity.read(tag); } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java index f0dda4960..4c8ca8e1e 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java @@ -26,15 +26,18 @@ import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.event.platform.PlatformReadyEvent; import com.sk89q.worldedit.extension.platform.Platform; +import com.sk89q.worldedit.forge.net.handler.InternalPacketHandler; import com.sk89q.worldedit.forge.net.handler.WECUIPacketHandler; import com.sk89q.worldedit.forge.net.packet.LeftClickAirEventMessage; -import com.sk89q.worldedit.forge.net.handler.InternalPacketHandler; import com.sk89q.worldedit.util.Location; +import com.sk89q.worldedit.world.block.BlockCategory; import com.sk89q.worldedit.world.block.BlockType; -import com.sk89q.worldedit.world.block.BlockTypes; +import com.sk89q.worldedit.world.entity.EntityType; +import com.sk89q.worldedit.world.item.ItemCategory; import com.sk89q.worldedit.world.item.ItemType; -import com.sk89q.worldedit.world.item.ItemTypes; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.tags.BlockTags; +import net.minecraft.tags.ItemTags; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; @@ -44,7 +47,6 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickEmpty import net.minecraftforge.eventbus.api.Event; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.DistExecutor; -import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent; @@ -120,19 +122,29 @@ public class ForgeWorldEdit { this.provider = new ForgePermissionsProvider.VanillaPermissionsProvider(platform); // } - // TODO Setup states - for (ResourceLocation name : ForgeRegistries.BLOCKS.getKeys()) { - String nameStr = name.toString(); - if (!BlockType.REGISTRY.keySet().contains(nameStr)) { - BlockType.REGISTRY.register(nameStr, new BlockType(nameStr)); - } - } + setupRegistries(); + } + private void setupRegistries() { + // Blocks + for (ResourceLocation name : ForgeRegistries.BLOCKS.getKeys()) { + BlockType.REGISTRY.register(name.toString(), new BlockType(name.toString(), + input -> ForgeAdapter.adapt(ForgeAdapter.adapt(input.getBlockType()).getDefaultState()))); + } + // Items for (ResourceLocation name : ForgeRegistries.ITEMS.getKeys()) { - String nameStr = name.toString(); - if (!ItemType.REGISTRY.keySet().contains(nameStr)) { - ItemType.REGISTRY.register(nameStr, new ItemType(nameStr)); - } + ItemType.REGISTRY.register(name.toString(), new ItemType(name.toString())); + } + // Entities + for (ResourceLocation name : ForgeRegistries.ENTITIES.getKeys()) { + EntityType.REGISTRY.register(name.toString(), new EntityType(name.toString())); + } + // Tags + for (ResourceLocation name : BlockTags.getCollection().getRegisteredTags()) { + BlockCategory.REGISTRY.register(name.toString(), new BlockCategory(name.toString())); + } + for (ResourceLocation name : ItemTags.getCollection().getRegisteredTags()) { + ItemCategory.REGISTRY.register(name.toString(), new ItemCategory(name.toString())); } } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/NBTConverter.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/NBTConverter.java index f05fbef47..bae43851c 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/NBTConverter.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/NBTConverter.java @@ -140,7 +140,7 @@ final class NBTConverter { public static NBTTagCompound toNative(CompoundTag tag) { NBTTagCompound compound = new NBTTagCompound(); for (Entry child : tag.getValue().entrySet()) { - compound.setTag(child.getKey(), toNative(child.getValue())); + compound.put(child.getKey(), toNative(child.getValue())); } return compound; } @@ -245,7 +245,7 @@ final class NBTConverter { Set tags = other.keySet(); Map map = new HashMap<>(); for (String tagName : tags) { - map.put(tagName, fromNative(other.getTag(tagName))); + map.put(tagName, fromNative(other.get(tagName))); } return new CompoundTag(map); } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/TileEntityUtils.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/TileEntityUtils.java index 8a65feaf4..52f027f51 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/TileEntityUtils.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/TileEntityUtils.java @@ -52,9 +52,9 @@ final class TileEntityUtils { checkNotNull(tag); checkNotNull(position); - tag.setTag("x", new NBTTagInt(position.getBlockX())); - tag.setTag("y", new NBTTagInt(position.getBlockY())); - tag.setTag("z", new NBTTagInt(position.getBlockZ())); + tag.put("x", new NBTTagInt(position.getBlockX())); + tag.put("y", new NBTTagInt(position.getBlockY())); + tag.put("z", new NBTTagInt(position.getBlockZ())); return tag; }