Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
213 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeBlockRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.worldedit.sponge; | ||
|
||
import com.sk89q.worldedit.registry.state.Property; | ||
import com.sk89q.worldedit.util.formatting.text.Component; | ||
import com.sk89q.worldedit.world.block.BlockState; | ||
import com.sk89q.worldedit.world.block.BlockType; | ||
import com.sk89q.worldedit.world.registry.BundledBlockRegistry; | ||
import org.spongepowered.api.state.StateProperty; | ||
import org.spongepowered.api.world.schematic.PaletteTypes; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.OptionalInt; | ||
import java.util.TreeMap; | ||
import javax.annotation.Nullable; | ||
|
||
public class SpongeBlockRegistry extends BundledBlockRegistry { | ||
|
||
@Override | ||
public Component getRichName(BlockType blockType) { | ||
return SpongeTextAdapter.convert(SpongeAdapter.adapt(blockType).asComponent()); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Map<String, ? extends Property<?>> getProperties(BlockType blockType) { | ||
Map<String, Property<?>> map = new TreeMap<>(); | ||
org.spongepowered.api.block.BlockType type = SpongeAdapter.adapt(blockType); | ||
for (StateProperty<?> stateProperty : type.getStateProperties()) { | ||
map.put(stateProperty.getName(), SpongeAdapter.adaptProperty(stateProperty)); | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public OptionalInt getInternalBlockStateId(BlockState state) { | ||
// TODO Fixup when string parsing exists. This is pretty messy. | ||
org.spongepowered.api.block.BlockType spongeType = SpongeAdapter.adapt(state.getBlockType()); | ||
org.spongepowered.api.block.BlockState workingState = spongeType.getDefaultState(); | ||
for (Property<?> property : state.getBlockType().getProperties()) { | ||
StateProperty spongeProp = spongeType.getStatePropertyByName(property.getName()).get(); | ||
Object val = spongeProp.parseValue(state.getState(property).toString().toLowerCase(Locale.ENGLISH)).get(); | ||
workingState = (org.spongepowered.api.block.BlockState) workingState.withStateProperty(spongeProp, (Comparable) val).get(); | ||
} | ||
return OptionalInt.of(PaletteTypes.GLOBAL_BLOCKS.get().create().getOrAssign(workingState)); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePropertyAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.worldedit.sponge; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.sk89q.worldedit.registry.state.Property; | ||
import org.spongepowered.api.state.StateProperty; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import javax.annotation.Nullable; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
public class SpongePropertyAdapter<T extends Comparable<T>> implements Property<T> { | ||
|
||
private final StateProperty<T> property; | ||
private final List<T> values; | ||
|
||
public SpongePropertyAdapter(StateProperty<T> property) { | ||
this.property = property; | ||
this.values = ImmutableList.copyOf(property.getPossibleValues()); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.property.getName(); | ||
} | ||
|
||
@Override | ||
public List<T> getValues() { | ||
return this.values; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public T getValueFor(String string) throws IllegalArgumentException { | ||
Optional<T> val = property.parseValue(string); | ||
checkArgument(val.isPresent(), "%s has no value for %s", getName(), string); | ||
return val.get(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
return getName().equals(((Property<?>) o).getName()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return property.getName().hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters