From ddff8ecbb267300160ebe185e6b85560f616976c Mon Sep 17 00:00:00 2001 From: wizjany Date: Sun, 28 Aug 2016 02:48:29 -0400 Subject: [PATCH] Re-add and update ForgeItemRegistry. And fix code that depended on it. Reverts parts of e91859130c39fe2b32473da77a44da54c9bf2822 --- .../sk89q/worldedit/forge/ForgeAdapter.java | 16 ++++- .../worldedit/forge/ForgeItemRegistry.java | 49 +++++++++++++++ .../com/sk89q/worldedit/forge/ForgeWorld.java | 59 +++++++++---------- .../sk89q/worldedit/forge/ForgeWorldData.java | 7 +++ 4 files changed, 98 insertions(+), 33 deletions(-) create mode 100644 worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java 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 57b8e1f2e..42f40890f 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 @@ -20,8 +20,9 @@ package com.sk89q.worldedit.forge; import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.world.World; - +import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -46,6 +47,19 @@ final class ForgeAdapter { return new Vec3d(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()); } + public static EnumFacing adapt(Direction face) { + switch (face) { + case NORTH: return EnumFacing.NORTH; + case SOUTH: return EnumFacing.SOUTH; + case WEST: return EnumFacing.WEST; + case EAST: return EnumFacing.EAST; + case DOWN: return EnumFacing.DOWN; + case UP: + default: + return EnumFacing.UP; + } + } + public static BlockPos toBlockPos(Vector vector) { return new BlockPos(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()); } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java new file mode 100644 index 000000000..83222c029 --- /dev/null +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java @@ -0,0 +1,49 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package com.sk89q.worldedit.forge; + +import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.world.registry.ItemRegistry; +import net.minecraft.item.Item; +import net.minecraft.util.ResourceLocation; + +import javax.annotation.Nullable; + +public class ForgeItemRegistry implements ItemRegistry { + @Nullable + @Override + public BaseItem createFromId(String id) { + Item match = Item.REGISTRY.getObject(new ResourceLocation(id)); + if (match != null) { + return new BaseItem(Item.REGISTRY.getIDForObject(match), (short) 0); + } else { + return null; + } + } + + @Nullable + @Override + public BaseItem createFromId(int id) { + if (Item.REGISTRY.getObjectById(id) != null) { + return new BaseItem(id, (short) 0); + } else { + return null; + } + } +} 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 49a004a6d..d2a2d69ff 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 @@ -20,47 +20,36 @@ package com.sk89q.worldedit.forge; import com.sk89q.jnbt.CompoundTag; -import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.MaxChangedBlocksException; -import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.Vector2D; -import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.blocks.LazyBlock; import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.internal.Constants; import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.TreeGenerator.TreeType; import com.sk89q.worldedit.world.AbstractWorld; import com.sk89q.worldedit.world.biome.BaseBiome; import com.sk89q.worldedit.world.registry.WorldData; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.annotation.Nullable; -import net.minecraft.block.Block; -import net.minecraft.block.BlockLeaves; -import net.minecraft.block.BlockOldLeaf; -import net.minecraft.block.BlockOldLog; -import net.minecraft.block.BlockPlanks; +import net.minecraft.block.*; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityList; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Blocks; import net.minecraft.inventory.IInventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.play.server.SPacketChunkData; import net.minecraft.server.management.PlayerChunkMap; import net.minecraft.server.management.PlayerChunkMapEntry; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.World; @@ -70,19 +59,15 @@ import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.IChunkGenerator; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.ChunkProviderServer; -import net.minecraft.world.gen.feature.WorldGenBigMushroom; -import net.minecraft.world.gen.feature.WorldGenBigTree; -import net.minecraft.world.gen.feature.WorldGenBirchTree; -import net.minecraft.world.gen.feature.WorldGenCanopyTree; -import net.minecraft.world.gen.feature.WorldGenMegaJungle; -import net.minecraft.world.gen.feature.WorldGenMegaPineTree; -import net.minecraft.world.gen.feature.WorldGenSavannaTree; -import net.minecraft.world.gen.feature.WorldGenShrub; -import net.minecraft.world.gen.feature.WorldGenSwamp; -import net.minecraft.world.gen.feature.WorldGenTaiga1; -import net.minecraft.world.gen.feature.WorldGenTaiga2; -import net.minecraft.world.gen.feature.WorldGenTrees; -import net.minecraft.world.gen.feature.WorldGenerator; +import net.minecraft.world.gen.feature.*; + +import javax.annotation.Nullable; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.logging.Level; +import java.util.logging.Logger; import static com.google.common.base.Preconditions.checkNotNull; @@ -227,6 +212,16 @@ public class ForgeWorld extends AbstractWorld { return false; } + @Override + public boolean useItem(Vector position, BaseItem item, Direction face) { + Item nativeItem = Item.getItemById(item.getType()); + ItemStack stack = new ItemStack(nativeItem, 1, item.getData()); + World world = getWorld(); + EnumActionResult used = stack.onItemUse(new WorldEditFakePlayer((WorldServer) world), world, ForgeAdapter.toBlockPos(position), + EnumHand.MAIN_HAND, ForgeAdapter.adapt(face), 0, 0, 0); + return used != EnumActionResult.FAIL; + } + @Override public void dropItem(Vector position, BaseItemStack item) { checkNotNull(position); diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldData.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldData.java index 249e93ddc..de23b9a85 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldData.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldData.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.forge; import com.sk89q.worldedit.world.registry.BiomeRegistry; +import com.sk89q.worldedit.world.registry.ItemRegistry; import com.sk89q.worldedit.world.registry.LegacyWorldData; /** @@ -29,12 +30,18 @@ class ForgeWorldData extends LegacyWorldData { private static final ForgeWorldData INSTANCE = new ForgeWorldData(); private final BiomeRegistry biomeRegistry = new ForgeBiomeRegistry(); + private final ItemRegistry itemRegistry = new ForgeItemRegistry(); @Override public BiomeRegistry getBiomeRegistry() { return biomeRegistry; } + @Override + public ItemRegistry getItemRegistry() { + return itemRegistry; + } + /** * Get a static instance. *