Skip to content

Commit

Permalink
Various fixes and improvements for Forge WE 1.13.
Browse files Browse the repository at this point in the history
  • Loading branch information
wizjany committed Jun 10, 2019
1 parent f2f9c26 commit c361da1
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.collect.ImmutableList;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
Expand Down Expand Up @@ -210,11 +211,23 @@ public static ItemStack adapt(BaseItemStack baseItemStack) {
if (baseItemStack.getNbtData() != null) {
forgeCompound = NBTConverter.toNative(baseItemStack.getNbtData());
}
return new ItemStack(adapt(baseItemStack.getType()), baseItemStack.getAmount(), forgeCompound);
final ItemStack itemStack = new ItemStack(adapt(baseItemStack.getType()), baseItemStack.getAmount());
itemStack.setTag(forgeCompound);
return itemStack;
}

public static BaseItemStack adapt(ItemStack itemStack) {
CompoundTag tag = NBTConverter.fromNative(itemStack.serializeNBT());
if (tag.getValue().isEmpty()) {
tag = null;
} else {
final Tag tagTag = tag.getValue().get("tag");
if (tagTag instanceof CompoundTag) {
tag = ((CompoundTag) tagTag);
} else {
tag = null;
}
}
return new BaseItemStack(adapt(itemStack.getItem()), tag, itemStack.getCount());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.forge;

import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.registry.BlockCategoryRegistry;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.Tag;
import net.minecraft.util.ResourceLocation;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class ForgeBlockCategoryRegistry implements BlockCategoryRegistry {
@Override
public Set<BlockType> getCategorisedByName(String category) {
return Optional.ofNullable(BlockTags.getCollection().get(new ResourceLocation(category)))
.map(Tag::getAllElements).orElse(Collections.emptySet())
.stream().map(ForgeAdapter::adapt).collect(Collectors.toSet());
}

@Override
public Set<BlockType> getAll(Category<BlockType> category) {
return getCategorisedByName(category.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.state.IProperty;
import net.minecraftforge.fml.loading.FMLLoader;

import java.util.Collection;
import java.util.HashMap;
Expand All @@ -42,7 +43,7 @@ public class ForgeBlockRegistry extends BundledBlockRegistry {
@Override
public String getName(BlockType blockType) {
Block block = ForgeAdapter.adapt(blockType);
if (block != null) {
if (block != null && FMLLoader.getDist().isClient()) {
return block.getNameTextComponent().getFormattedText();
} else {
return super.getName(blockType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ public ForgeDataFixer(int dataVersion) {
INSTANCE = this;
registerConverters();
registerInspectors();
this.fixer = new WrappedDataFixer(DataFixesManager.getDataFixer());
}


// Called after fixers are built and ready for FIXING
@Override
public DataFixer build(final Executor executor) {
return this.fixer = new WrappedDataFixer(DataFixesManager.getDataFixer());
return fixer;
}

private class WrappedDataFixer implements DataFixer {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.forge;

import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.registry.ItemCategoryRegistry;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.Tag;
import net.minecraft.util.ResourceLocation;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class ForgeItemCategoryRegistry implements ItemCategoryRegistry {
@Override
public Set<ItemType> getCategorisedByName(String category) {
return Optional.ofNullable(ItemTags.getCollection().get(new ResourceLocation(category)))
.map(Tag::getAllElements).orElse(Collections.emptySet())
.stream().map(ForgeAdapter::adapt).collect(Collectors.toSet());
}

@Override
public Set<ItemType> getAll(Category<ItemType> category) {
return getCategorisedByName(category.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.registry.BundledItemRegistry;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.registries.RegistryManager;

import javax.annotation.Nullable;

Expand All @@ -29,6 +34,13 @@ public class ForgeItemRegistry extends BundledItemRegistry {
@Nullable
@Override
public String getName(ItemType itemType) {
return super.getName(itemType); // TODO
if (FMLLoader.getDist().isClient()) {
final Item item = RegistryManager.ACTIVE.getRegistry(Item.class)
.getValue(ResourceLocation.tryCreate(itemType.getId()));
if (item != null) {
return I18n.format(item.getTranslationKey());
}
}
return super.getName(itemType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.sk89q.worldedit.forge;

import com.sk89q.jnbt.CompoundTag;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
Expand All @@ -33,12 +34,16 @@
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.serializer.gson.GsonComponentSerializer;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
import io.netty.buffer.Unpooled;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SPacketBlockChange;
import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.EnumHand;
Expand All @@ -48,12 +53,14 @@
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;

import java.io.IOException;
import java.util.UUID;

import javax.annotation.Nullable;

public class ForgePlayer extends AbstractPlayerActor {

private static final int STRUCTURE_BLOCK_PACKET_ID = 7;
private final EntityPlayerMP player;

protected ForgePlayer(EntityPlayerMP player) {
Expand Down Expand Up @@ -183,18 +190,35 @@ public <T> T getFacet(Class<? extends T> cls) {

@Override
public <B extends BlockStateHolder<B>> void sendFakeBlock(BlockVector3 pos, B block) {
World world = getWorld();
if (!(world instanceof ForgeWorld)) {
return;
}
BlockPos loc = ForgeAdapter.toBlockPos(pos);
if (block == null) {
// TODO
// player.sendBlockChange(loc, player.getWorld().getBlockAt(loc).getBlockData());
final SPacketBlockChange packetOut = new SPacketBlockChange(((ForgeWorld) world).getWorld(), loc);
player.connection.sendPacket(packetOut);
} else {
// TODO
// player.sendBlockChange(loc, BukkitAdapter.adapt(block));
if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) {
player.connection.sendPacket(new SPacketUpdateTileEntity(
new BlockPos(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()), 7,
NBTConverter.toNative(((BaseBlock) block).getNbtData()))
);
final SPacketBlockChange packetOut = new SPacketBlockChange();
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
buf.writeBlockPos(loc);
buf.writeVarInt(Block.getStateId(ForgeAdapter.adapt(block.toImmutableState())));
try {
packetOut.readPacketData(buf);
} catch (IOException e) {
return;
}
player.connection.sendPacket(packetOut);
if (block instanceof BaseBlock && block.getBlockType().equals(BlockTypes.STRUCTURE_BLOCK)) {
final BaseBlock baseBlock = (BaseBlock) block;
final CompoundTag nbtData = baseBlock.getNbtData();
if (nbtData != null) {
player.connection.sendPacket(new SPacketUpdateTileEntity(
new BlockPos(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()),
STRUCTURE_BLOCK_PACKET_ID,
NBTConverter.toNative(nbtData))
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package com.sk89q.worldedit.forge;

import com.sk89q.worldedit.world.registry.BiomeRegistry;
import com.sk89q.worldedit.world.registry.BlockCategoryRegistry;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import com.sk89q.worldedit.world.registry.BundledRegistries;
import com.sk89q.worldedit.world.registry.ItemCategoryRegistry;
import com.sk89q.worldedit.world.registry.ItemRegistry;

/**
Expand All @@ -33,6 +35,8 @@ class ForgeRegistries extends BundledRegistries {
private final BlockRegistry blockRegistry = new ForgeBlockRegistry();
private final BiomeRegistry biomeRegistry = new ForgeBiomeRegistry();
private final ItemRegistry itemRegistry = new ForgeItemRegistry();
private final BlockCategoryRegistry blockCategoryRegistry = new ForgeBlockCategoryRegistry();
private final ItemCategoryRegistry itemCategoryRegistry = new ForgeItemCategoryRegistry();

@Override
public BlockRegistry getBlockRegistry() {
Expand All @@ -49,6 +53,16 @@ public ItemRegistry getItemRegistry() {
return itemRegistry;
}

@Override
public BlockCategoryRegistry getBlockCategoryRegistry() {
return blockCategoryRegistry;
}

@Override
public ItemCategoryRegistry getItemCategoryRegistry() {
return itemCategoryRegistry;
}

/**
* Get a static instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
Expand Down Expand Up @@ -243,25 +246,26 @@ public boolean setBiome(BlockVector2 position, BiomeType biome) {

@Override
public boolean useItem(BlockVector3 position, BaseItem item, Direction face) {
Item nativeItem = ForgeAdapter.adapt(item.getType());
ItemStack stack;
if (item.getNbtData() == null) {
stack = new ItemStack(nativeItem, 1);
} else {
stack = new ItemStack(nativeItem, 1, NBTConverter.toNative(item.getNbtData()));
}
ItemStack stack = ForgeAdapter.adapt(new BaseItemStack(item.getType(), item.getNbtData(), 1));
World world = getWorld();
ItemUseContext itemUseContext = new ItemUseContext(
new WorldEditFakePlayer((WorldServer) world),
stack,
ForgeAdapter.toBlockPos(position),
ForgeAdapter.adapt(face),
0f,
0f,
0f
);
final WorldEditFakePlayer fakePlayer = new WorldEditFakePlayer((WorldServer) world);
fakePlayer.setHeldItem(EnumHand.MAIN_HAND, stack);
fakePlayer.setLocationAndAngles(position.getBlockX(), position.getBlockY(), position.getBlockZ(),
(float) face.toVector().toYaw(), (float) face.toVector().toPitch());
final BlockPos blockPos = ForgeAdapter.toBlockPos(position);
final EnumFacing enumFacing = ForgeAdapter.adapt(face);
ItemUseContext itemUseContext = new ItemUseContext(fakePlayer, stack, blockPos, enumFacing, blockPos.getX(), blockPos.getY(), blockPos.getZ());
EnumActionResult used = stack.onItemUse(itemUseContext);
return used != EnumActionResult.FAIL;
if (used != EnumActionResult.SUCCESS) {
// try activating the block
if (getWorld().getBlockState(blockPos).onBlockActivated(world, blockPos, fakePlayer, EnumHand.MAIN_HAND,
enumFacing, blockPos.getX(), blockPos.getY(), blockPos.getZ())) {
used = EnumActionResult.SUCCESS;
} else {
used = stack.getItem().onItemRightClick(world, fakePlayer, EnumHand.MAIN_HAND).getType();
}
}
return used == EnumActionResult.SUCCESS;
}

@Override
Expand Down
Loading

0 comments on commit c361da1

Please sign in to comment.