Permalink
Browse files

Port Item Dispenser IC.

  • Loading branch information...
me4502 committed Jan 8, 2018
1 parent fb7f710 commit 1f845515016b7cc5b436b4ca0b3285ece725a893
@@ -44,6 +44,7 @@
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.XorGate;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.block.BlockReplacer;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.entity.EntitySpawner;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.entity.ItemDispenser;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.miscellaneous.ProgrammableFireworksDisplay;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.miscellaneous.WirelessReceiver;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.world.miscellaneous.WirelessTransmitter;
@@ -81,6 +82,7 @@
registerICType(new ICType<>("MC1111", "RECEIVER", "Wireless Receiver", "Receives a wireless redstone signal.", new WirelessReceiver.Factory()));
registerICType(new ICType<>("MC1200", "SPAWNER", "Entity Spawner", "Spawns an entity with specified data.", new EntitySpawner.Factory()));
registerICType(new ICType<>("MC1201", "DISPENSER", "Item Dispenser", "Spawns in items with specified data.", new ItemDispenser.Factory()));
registerICType(new ICType<>("MC1203", "ZEUS BOLT", "Zeus Bolt", "Strikes a location with lightning.", new ZeusBolt.Factory()));
@@ -63,7 +63,13 @@ public void create(Player player, List<Text> lines) throws InvalidICException {
try {
amount = Integer.parseInt(SignUtil.getTextRaw(lines.get(3)));
if (amount < 1) {
throw new InvalidICException("You can't spawn less than 1 entity!");
} else if (amount > 100) {
throw new InvalidICException("You can't spawn more than 100 entities at a time!");
}
} catch (Exception e) {
throw new InvalidICException("Invalid amount on 4th line!");
}
lines.set(3, Text.of(amount));
@@ -0,0 +1,166 @@
/*
* 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.mechanics.ics.chips.world.entity;
import com.google.common.collect.Lists;
import com.sk89q.craftbook.sponge.mechanics.ics.IC;
import com.sk89q.craftbook.sponge.mechanics.ics.InvalidICException;
import com.sk89q.craftbook.sponge.mechanics.ics.SerializedICData;
import com.sk89q.craftbook.sponge.mechanics.ics.factory.ICFactory;
import com.sk89q.craftbook.sponge.mechanics.ics.factory.SerializedICFactory;
import com.sk89q.craftbook.sponge.util.SignUtil;
import com.sk89q.craftbook.sponge.util.prompt.ItemStackSnapshotDataPrompt;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.data.property.block.MatterProperty;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.List;
import java.util.Optional;
public class ItemDispenser extends IC {
private static ItemStackSnapshotDataPrompt ITEMS_PROMPT = new ItemStackSnapshotDataPrompt(
1, 9, "Enter Items to Dispense"
);
private ItemDispenser.Factory.ItemStackData itemStackData;
public ItemDispenser(ICFactory<ItemDispenser> icFactory, Location<World> block) {
super(icFactory, block);
}
@Override
public void create(Player player, List<Text> lines) throws InvalidICException {
super.create(player, lines);
itemStackData = new ItemDispenser.Factory.ItemStackData();
String line2 = SignUtil.getTextRaw(lines.get(2));
if (!line2.isEmpty()) {
Sponge.getRegistry().getType(ItemType.class, line2).ifPresent(itemType ->
itemStackData.itemStacks = Lists.newArrayList(itemType.getTemplate())
);
}
if (itemStackData.itemStacks.isEmpty()) {
ITEMS_PROMPT.getData(player, entityArchetypes -> {
itemStackData.itemStacks = entityArchetypes;
setLine(2, Text.of(itemStackData.itemStacks.get(0).getType().getName()));
});
}
}
@Override
public void trigger() {
if (getPinSet().getInput(0, this)) {
if (!itemStackData.itemStacks.isEmpty()) {
Location<World> block = getBackBlock();
while (block.getBlockType().getProperty(MatterProperty.class).map(MatterProperty::getValue).orElse(MatterProperty.Matter.GAS) == MatterProperty.Matter.SOLID) {
if (block.getY() >= 255) {
break;
}
block = block.getRelative(Direction.UP);
}
for (ItemStackSnapshot stack : itemStackData.itemStacks) {
Item item = (Item) block.getExtent().createEntity(EntityTypes.ITEM, block.getPosition());
item.offer(Keys.REPRESENTED_ITEM, stack);
block.getExtent().spawnEntity(item);
}
}
}
}
public static class Factory extends SerializedICFactory<ItemDispenser, ItemDispenser.Factory.ItemStackData> {
public Factory() {
super(ItemDispenser.Factory.ItemStackData.class, 1);
}
@Override
public ItemDispenser createInstance(Location<World> location) {
return new ItemDispenser(this, location);
}
@Override
public String[] getLineHelp() {
return new String[] {
"item type, or blank to set later",
""
};
}
@Override
public String[][] getPinHelp() {
return new String[][] {
new String[] {
"Clock"
},
new String[] {
"None"
}
};
}
@Override
public void setData(ItemDispenser ic, ItemDispenser.Factory.ItemStackData data) {
ic.itemStackData = data;
}
@Override
public ItemDispenser.Factory.ItemStackData getData(ItemDispenser ic) {
return ic.itemStackData;
}
@Override
protected Optional<ItemDispenser.Factory.ItemStackData> buildContent(DataView container) throws InvalidDataException {
ItemDispenser.Factory.ItemStackData itemStackData = new ItemDispenser.Factory.ItemStackData();
itemStackData.itemStacks = container.getSerializableList(DataQuery.of("ItemStacks"), ItemStackSnapshot.class).orElse(Lists.newArrayList());
return Optional.of(itemStackData);
}
public static class ItemStackData extends SerializedICData {
public List<ItemStackSnapshot> itemStacks = Lists.newArrayList();
@Override
public int getContentVersion() {
return 1;
}
@Override
public DataContainer toContainer() {
return super.toContainer()
.set(DataQuery.of("ItemStacks"), this.itemStacks);
}
}
}
}
@@ -0,0 +1,43 @@
/*
* 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.entity.living.player.Player;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
public class ItemStackSnapshotDataPrompt extends DataPrompt<ItemStackSnapshot> {
public ItemStackSnapshotDataPrompt(int minSize, int maxSize, String title) {
super(minSize, maxSize, title);
}
public ItemStackSnapshotDataPrompt(int minSize, int maxSize, String title, @Nullable BiPredicate<Player, List<ItemStack>> customValidityCheck) {
super(minSize, maxSize, title, customValidityCheck);
}
@Override
public List<ItemStackSnapshot> convertData(List<ItemStack> items) {
return items.stream().map(ItemStack::createSnapshot).collect(Collectors.toList());
}
}

0 comments on commit 1f84551

Please sign in to comment.