Permalink
Browse files

Update forge for new block API

  • Loading branch information...
kenzierocks committed Oct 5, 2018
1 parent d0ea512 commit ef2c07ca249b2e88e4944210563ea887c59a7892
@@ -51,6 +51,11 @@ public void setName(final String name) {
this.name = name;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{name=" + name + "}";
}
@Override
public int hashCode() {
return name.hashCode();
@@ -168,6 +168,17 @@ public BlockState getDefaultState() {
return ImmutableList.copyOf(getBlockStatesMap().values());
}
/**
* Gets a state of this BlockType with the given properties.
*
* @return The state, if it exists
*/
public BlockState getState(Map<Property<?>, Object> key) {
BlockState state = getBlockStatesMap().get(key);
checkArgument(state != null, "%s has no state for %s", this, key);
return state;
}
/**
* Gets whether this block type has an item representation.
*
@@ -13,6 +13,11 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle.forge'
configurations.all {
resolutionStrategy {
force 'org.ow2.asm:asm:5.2'
}
}
dependencies {
compile project(':worldedit-core')
@@ -19,13 +19,27 @@
package com.sk89q.worldedit.forge;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.registry.state.BooleanProperty;
import com.sk89q.worldedit.registry.state.DirectionalProperty;
import com.sk89q.worldedit.registry.state.EnumProperty;
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 net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.stream.Collectors;
final class ForgeAdapter {
private ForgeAdapter() {
@@ -60,8 +74,41 @@ public static EnumFacing adapt(Direction face) {
}
}
public static Direction adaptEnumFacing(EnumFacing face) {
switch (face) {
case NORTH: return Direction.NORTH;
case SOUTH: return Direction.SOUTH;
case WEST: return Direction.WEST;
case EAST: return Direction.EAST;
case DOWN: return Direction.DOWN;
case UP:
default:
return Direction.UP;
}
}
public static BlockPos toBlockPos(Vector vector) {
return new BlockPos(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ());
}
public static Property<?> adaptProperty(IProperty<?> property) {
if (property instanceof PropertyBool) {
return new BooleanProperty(property.getName(), ImmutableList.copyOf(((PropertyBool) property).getAllowedValues()));
}
if (property instanceof PropertyInteger) {
return new IntegerProperty(property.getName(), ImmutableList.copyOf(((PropertyInteger) property).getAllowedValues()));
}
if (property instanceof PropertyDirection) {
return new DirectionalProperty(property.getName(), ((PropertyDirection) property).getAllowedValues().stream()
.map(ForgeAdapter::adaptEnumFacing)
.collect(Collectors.toList()));
}
if (property instanceof PropertyEnum) {
return new EnumProperty(property.getName(), ((PropertyEnum<?>) property).getAllowedValues().stream()
.map(e -> e.getName())
.collect(Collectors.toList()));
}
return new IPropertyAdapter<>(property);
}
}
@@ -0,0 +1,94 @@
/*
* 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.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.PassthroughBlockMaterial;
import net.minecraft.block.material.EnumPushReaction;
import net.minecraft.block.material.Material;
import javax.annotation.Nullable;
/**
* Forge block material that pulls as much info as possible from the Minecraft
* Material, and passes the rest to another implementation, typically the
* bundled block info.
*/
public class ForgeBlockMaterial extends PassthroughBlockMaterial {
private final Material delegate;
public ForgeBlockMaterial(Material delegate, @Nullable BlockMaterial secondary) {
super(secondary);
this.delegate = delegate;
}
@Override
public boolean isAir() {
return delegate == Material.AIR || super.isAir();
}
@Override
public boolean isOpaque() {
return delegate.isOpaque();
}
@Override
public boolean isLiquid() {
return delegate.isLiquid();
}
@Override
public boolean isSolid() {
return delegate.isSolid();
}
@Override
public boolean isFragileWhenPushed() {
return delegate.getMobilityFlag() == EnumPushReaction.DESTROY;
}
@Override
public boolean isUnpushable() {
return delegate.getMobilityFlag() == EnumPushReaction.BLOCK;
}
@Override
public boolean isMovementBlocker() {
return delegate.blocksMovement();
}
@Override
public boolean isBurnable() {
return delegate.getCanBurn();
}
@Override
public boolean isToolRequired() {
return !delegate.isToolNotRequired();
}
@Override
public boolean isReplacedDuringPlacement() {
return delegate.isReplaceable();
}
}
@@ -0,0 +1,58 @@
/*
* 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.state.Property;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.BundledBlockRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class ForgeBlockRegistry extends BundledBlockRegistry {
private Map<Material, ForgeBlockMaterial> materialMap = new HashMap<>();
@Override
public BlockMaterial getMaterial(BlockType blockType) {
return materialMap.computeIfAbsent(Block.getBlockFromName(blockType.getId()).getDefaultState().getMaterial(),
m -> new ForgeBlockMaterial(m, super.getMaterial(blockType)));
}
@Override
public Map<String, ? extends Property<?>> getProperties(BlockType blockType) {
Map<String, Property<?>> map = new TreeMap<>();
Collection<IProperty<?>> propertyKeys = Block.getBlockFromName(blockType.getId())
.getDefaultState()
.getPropertyKeys();
for (IProperty<?> key : propertyKeys) {
map.put(key.getName(), ForgeAdapter.adaptProperty(key));
}
return map;
}
}
@@ -20,6 +20,7 @@
package com.sk89q.worldedit.forge;
import com.sk89q.worldedit.world.registry.BiomeRegistry;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import com.sk89q.worldedit.world.registry.BundledRegistries;
import com.sk89q.worldedit.world.registry.ItemRegistry;
@@ -29,9 +30,15 @@
class ForgeRegistries extends BundledRegistries {
private static final ForgeRegistries INSTANCE = new ForgeRegistries();
private final BlockRegistry blockRegistry = new ForgeBlockRegistry();
private final BiomeRegistry biomeRegistry = new ForgeBiomeRegistry();
private final ItemRegistry itemRegistry = new ForgeItemRegistry();
@Override
public BlockRegistry getBlockRegistry() {
return blockRegistry;
}
@Override
public BiomeRegistry getBiomeRegistry() {
return biomeRegistry;
Oops, something went wrong.

0 comments on commit ef2c07c

Please sign in to comment.