Permalink
Browse files

Refactored how block data is added to ICs.

Also fixed a key registration issue.
  • Loading branch information...
me4502 committed Jan 6, 2018
1 parent 6640639 commit ba1c5a613d160a820356c2dcb787582673c14b84
View
@@ -147,7 +147,7 @@ subprojects {
include '**/*.java'
exclude '**/Metrics.java'
ignoreFailures = true
ignoreFailures = false
newLine = false
}
@@ -25,6 +25,7 @@
import com.sk89q.craftbook.sponge.mechanics.ics.factory.SerializedICFactory;
import com.sk89q.craftbook.sponge.util.BlockUtil;
import com.sk89q.craftbook.sponge.util.SignUtil;
import com.sk89q.craftbook.sponge.util.prompt.BlockStateDataPrompt;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockTypes;
@@ -50,9 +51,19 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
public class BlockReplacer extends IC {
private static BlockStateDataPrompt OFF_BLOCK_PROMPT = new BlockStateDataPrompt(
1, 1, "Enter Off State"
);
private static BlockStateDataPrompt ON_BLOCK_PROMPT = new BlockStateDataPrompt(
1, 1, "Enter On State"
);
private Factory.BlockTypeData blockTypeData;
private int delay;
@@ -87,32 +98,10 @@ public void create(Player player, List<Text> lines) throws InvalidICException {
blockTypeData = new BlockReplacer.Factory.BlockTypeData();
Inventory offBlockInventory = Inventory.builder()
.of(InventoryArchetypes.CHEST)
.property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Enter Off Block")))
.property(InventoryDimension.PROPERTY_NAME, InventoryDimension.of(9, 1))
.listener(InteractInventoryEvent.Close.class, close -> {
Inventory inventory = close.getTargetInventory();
inventory.peek().ifPresent(itemStack ->
blockTypeData.offBlock = itemStack.get(Keys.ITEM_BLOCKSTATE).orElse(itemStack.getType().getBlock().orElse(BlockTypes.AIR).getDefaultState()));
})
.build(CraftBookPlugin.spongeInst().container);
Inventory onBlockInventory = Inventory.builder()
.of(InventoryArchetypes.CHEST)
.property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Enter On Block")))
.property(InventoryDimension.PROPERTY_NAME, InventoryDimension.of(9, 1))
.listener(InteractInventoryEvent.Close.class, close -> {
Inventory inventory = close.getTargetInventory();
inventory.peek().ifPresent(itemStack -> {
blockTypeData.onBlock = itemStack.get(Keys.ITEM_BLOCKSTATE).orElse(itemStack.getType().getBlock().orElse(BlockTypes.AIR).getDefaultState());
player.openInventory(offBlockInventory);
});
})
.build(CraftBookPlugin.spongeInst().container);
player.openInventory(onBlockInventory);
ON_BLOCK_PROMPT.getData(player, blockStates -> {
blockTypeData.onBlock = blockStates.get(0);
OFF_BLOCK_PROMPT.getData(player, blockStates1 -> blockTypeData.offBlock = blockStates1.get(0));
});
}
@Override
@@ -150,7 +139,6 @@ public void trigger() {
Set<Location> traversedBlocks = new HashSet<>();
traversedBlocks.add(block);
getPinSet().setOutput(0, replaceBlocks(getPinSet().getInput(0, this), block, traversedBlocks), this);
}
}
@@ -39,6 +39,9 @@
public class CraftBookData {
public static void registerData() {
// This registers the keys.
new CraftBookKeys();
// Generic Data
DataRegistration<LastPowerData, ImmutableLastPowerData> lastPowerData =
DataRegistration.<LastPowerData, ImmutableLastPowerData>builder()
@@ -17,7 +17,6 @@
package com.sk89q.craftbook.sponge.util.data;
import static org.spongepowered.api.data.DataQuery.of;
import static org.spongepowered.api.data.key.KeyFactory.makeSingleKey;
import com.google.common.reflect.TypeToken;
import com.sk89q.craftbook.sponge.mechanics.blockbags.EmbeddedBlockBag;
@@ -29,19 +28,41 @@
public class CraftBookKeys {
public static Key<MutableBoundedValue<Integer>> LAST_POWER = makeSingleKey(new TypeTokens.IntegerTypeToken(),
new TypeTokens.MutableBoundedValueIntegerTypeToken(), of("LastPower"), "craftbook:lastpower", "LastPower");
public CraftBookKeys() {
}
public static Key<Value<SerializedICData>> IC_DATA = makeSingleKey(new TypeTokens.ICTypeToken(),
new TypeTokens.ICValueTypeToken(), of("IC"), "craftbook:ic", "IC");
public static Key<MutableBoundedValue<Integer>> LAST_POWER = Key.builder()
.type(new TypeTokens.MutableBoundedValueIntegerTypeToken())
.id("craftbook:lastpower")
.name("LastPower")
.query(of("LastPower"))
.build();
public static Key<Value<String>> NAMESPACE = makeSingleKey(TypeToken.of(String.class),
new TypeToken<Value<String>>(){}, of("Namespace"), "craftbook:namespace", "Namespace");
public static Key<Value<SerializedICData>> IC_DATA = Key.builder()
.type(new TypeTokens.ICValueTypeToken())
.id("craftbook:ic")
.name("IC")
.query(of("IC"))
.build();
public static Key<Value<Long>> BLOCK_BAG = makeSingleKey(new TypeTokens.LongTypeToken(),
new TypeTokens.LongValueTypeToken(), of("BlockBag"), "craftbook:blockbag", "BlockBag");
public static Key<Value<String>> NAMESPACE = Key.builder()
.type(new TypeToken<Value<String>>(){})
.id("craftbook:namespace")
.name("Namespace")
.query(of("Namespace"))
.build();
public static Key<Value<EmbeddedBlockBag>> EMBEDDED_BLOCK_BAG = makeSingleKey(new TypeToken<EmbeddedBlockBag>() {},
new TypeToken<Value<EmbeddedBlockBag>>() {}, of("EmbeddedBlockBag"), "craftbook:embeddedblockbag",
"EmbeddedBlockBag");
public static Key<Value<Long>> BLOCK_BAG = Key.builder()
.type(new TypeTokens.LongValueTypeToken())
.id("craftbook:blockbag")
.name("BlockBag")
.query(of("BlockBag"))
.build();
public static Key<Value<EmbeddedBlockBag>> EMBEDDED_BLOCK_BAG = Key.builder()
.type(new TypeToken<Value<EmbeddedBlockBag>>() {})
.id("craftbook:embeddedblockbag")
.name("EmbeddedBlockBag")
.query(of("EmbeddedBlockBag"))
.build();
}
@@ -0,0 +1,64 @@
/*
* CraftBook Copyright (C) 2010-2018 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2018 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.util.prompt;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.Text;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
public class BlockStateDataPrompt extends DataPrompt<BlockState> {
public BlockStateDataPrompt(int minSize, int maxSize, String title) {
super(minSize, maxSize, title);
}
public BlockStateDataPrompt(int minSize, int maxSize, String title, @Nullable BiPredicate<Player, List<ItemStack>> customValidityCheck) {
super(minSize, maxSize, title, customValidityCheck);
}
@Override
public boolean isValid(Player player, List<ItemStack> items) {
if (!super.isValid(player, items)) {
return false;
}
for (ItemStack item : items) {
if (!item.supports(Keys.ITEM_BLOCKSTATE) && !item.getType().getBlock().isPresent()) {
player.sendMessage(Text.of("Item '" + item.getType().getName() + "' is not a block!"));
return false;
}
}
return true;
}
@Override
public List<BlockState> convertData(List<ItemStack> items) {
return items.stream()
.map(item -> item.get(Keys.ITEM_BLOCKSTATE).orElse(item.getType().getBlock().orElse(BlockTypes.AIR).getDefaultState()))
.collect(Collectors.toList());
}
}
@@ -0,0 +1,109 @@
/*
* CraftBook Copyright (C) 2010-2018 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2018 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.util.prompt;
import com.sk89q.craftbook.sponge.CraftBookPlugin;
import org.spongepowered.api.data.DataSerializable;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.item.inventory.InteractInventoryEvent;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryArchetypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.property.InventoryDimension;
import org.spongepowered.api.item.inventory.property.InventoryTitle;
import org.spongepowered.api.text.Text;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import javax.annotation.Nullable;
public abstract class DataPrompt<T extends DataSerializable> {
private int minSize;
private int maxSize;
private String title;
@Nullable private BiPredicate<Player, List<ItemStack>> customValidityCheck;
public DataPrompt(int minSize, int maxSize, String title) {
this.minSize = minSize;
this.maxSize = maxSize;
this.title = title;
}
public DataPrompt(int minSize, int maxSize, String title, @Nullable BiPredicate<Player, List<ItemStack>> customValidityCheck) {
this.minSize = minSize;
this.maxSize = maxSize;
this.title = title;
this.customValidityCheck = customValidityCheck;
}
private void createInventory(Player player, Consumer<List<ItemStack>> consumer) {
int height = (int) Math.ceil(maxSize / 9.0);
int width = 9; //maxSize >= 9 ? 9 : maxSize;
Consumer<InteractInventoryEvent.Close> closeListener = close -> {
Inventory closedInventory = close.getTargetInventory();
Optional<ItemStack> itemStack;
List<ItemStack> foundItems = new ArrayList<>();
while ((itemStack = closedInventory.first().poll()).isPresent()) {
foundItems.add(itemStack.get());
}
consumer.accept(foundItems);
};
Inventory inventory = Inventory.builder()
.of(InventoryArchetypes.CHEST)
.property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of(this.title)))
.property(InventoryDimension.PROPERTY_NAME, InventoryDimension.of(width, height))
.listener(InteractInventoryEvent.Close.class, closeListener)
.build(CraftBookPlugin.spongeInst().container);
player.openInventory(inventory);
}
public boolean isValid(Player player, List<ItemStack> items) {
if (items.isEmpty()) {
player.sendMessage(Text.of("You must provide an item!"));
return false;
}
if (items.size() > this.maxSize) {
player.sendMessage(Text.of("Provided too many items. Can only provide up to " + maxSize));
}
if (items.size() < this.minSize) {
player.sendMessage(Text.of("Provided too few items. Must provide at least " + minSize));
}
if (this.customValidityCheck != null && !this.customValidityCheck.test(player, items)) {
return false;
}
return true;
}
public abstract List<T> convertData(List<ItemStack> items);
public void getData(Player player, Consumer<List<T>> callback) {
createInventory(player, itemStacks -> {
if (isValid(player, itemStacks)) {
callback.accept(convertData(itemStacks));
}
});
}
}

0 comments on commit ba1c5a6

Please sign in to comment.