use of io.xol.chunkstories.api.item.Item in project chunkstories-api by Hugobros3.
the class BasicInventory method placeItemPileAt.
@Override
public ItemPile placeItemPileAt(int x, int y, ItemPile itemPile) {
ItemPile currentPileAtLocation = this.getItemPileAt(x, y);
// If empty and has space, put it in.
if (currentPileAtLocation == null && canPlaceItemAt(x, y, itemPile)) {
itemPile.setInventory(this);
itemPile.setX(x);
itemPile.setY(y);
contents[x % width][y % height] = itemPile;
// Push changes
this.refreshItemSlot(x, y, contents[x % width][y % height]);
// There is nothing left
return null;
}
// If the two piles are similar we can try to merge them
if (currentPileAtLocation != null && currentPileAtLocation.canMergeWith(itemPile) && !currentPileAtLocation.equals(itemPile)) {
Item item = currentPileAtLocation.getItem();
int currentAmount = currentPileAtLocation.getAmount();
int wouldBeAddedAmount = itemPile.getAmount();
// The existing pile is not already full
if (currentAmount < item.getDefinition().getMaxStackSize()) {
int totalAmount = currentAmount + wouldBeAddedAmount;
// How much can we add ?
int addableAmmount = Math.min(totalAmount, item.getDefinition().getMaxStackSize()) - currentAmount;
currentPileAtLocation.setAmount(currentAmount + addableAmmount);
// If we could add all to the first stack, discard the second pile
if (addableAmmount == wouldBeAddedAmount) {
// Push changes
this.refreshItemSlot(x, y, contents[x % width][y % height]);
return null;
} else // If we couldn't, reduce it's size
{
itemPile.setAmount(wouldBeAddedAmount - addableAmmount);
// Push changes
this.refreshItemSlot(x, y, contents[x % width][y % height]);
return itemPile;
}
}
}
// If none of the above can be done, return the original pile.
return itemPile;
}
use of io.xol.chunkstories.api.item.Item in project chunkstories by Hugobros3.
the class GiveCommand method handleCommand.
@Override
public boolean handleCommand(CommandEmitter emitter, Command command, String[] arguments) {
if (!emitter.hasPermission("server.give")) {
emitter.sendMessage("You don't have the permission.");
return true;
}
if (!(emitter instanceof Player)) {
emitter.sendMessage("You need to be a player to use this command.");
return true;
}
Content gameContent = server.getContent();
Player player = (Player) emitter;
if (arguments.length == 0) {
player.sendMessage("#FF969BSyntax : /give <item> [amount] [to]");
return true;
}
int amount = 1;
Player to = player;
String itemName = arguments[0];
// Look for the item first
ItemDefinition type = gameContent.items().getItemDefinition(itemName);
// If the type was found we are simply trying to spawn an item
Item item = null;
if (type != null)
item = type.newItem();
else {
String voxelName = itemName;
int voxelMeta = 0;
if (voxelName.contains(":")) {
voxelMeta = Integer.parseInt(voxelName.split(":")[1]);
voxelName = voxelName.split(":")[0];
}
// Try to find a matching voxel
Voxel voxel = gameContent.voxels().getVoxel(itemName);
if (voxel != null) {
// Spawn new itemPile in his inventory
ItemVoxel itemVoxel = (ItemVoxel) gameContent.items().getItemDefinition("item_voxel").newItem();
itemVoxel.voxel = voxel;
itemVoxel.voxelMeta = voxelMeta;
item = itemVoxel;
}
}
if (item == null) {
player.sendMessage("#FF969BItem or voxel \"" + arguments[0] + " can't be found.");
return true;
}
if (arguments.length >= 2) {
amount = Integer.parseInt(arguments[1]);
}
if (arguments.length >= 3) {
if (gameContent instanceof ServerInterface)
to = ((ServerInterface) gameContent).getPlayerByName(arguments[2]);
else {
player.sendMessage("#FF969BThis is a singleplayer world - there are no other players");
return true;
}
}
if (to == null) {
player.sendMessage("#FF969BPlayer \"" + arguments[2] + " can't be found.");
return true;
}
ItemPile itemPile = new ItemPile(item);
itemPile.setAmount(amount);
((EntityWithInventory) to.getControlledEntity()).getInventory().addItemPile(itemPile);
player.sendMessage("#FF969BGave " + (amount > 1 ? amount + "x " : "") + "#4CFF00" + itemPile.getItem().getName() + " #FF969Bto " + to.getDisplayName());
return true;
}
Aggregations