use of io.xol.chunkstories.api.exceptions.UndefinedItemTypeException in project chunkstories-api by Hugobros3.
the class ItemPile method obtainItemPileFromStream.
public static ItemPile obtainItemPileFromStream(ContentTranslator translator, DataInputStream stream) throws IOException, UndefinedItemTypeException, NullItemException {
int itemId = stream.readInt();
if (itemId == 0)
throw new NullItemException(stream);
ItemDefinition itemType = translator.getItemForId(itemId);
if (itemType == null)
throw new UndefinedItemTypeException(itemId);
ItemPile itemPile = new ItemPile(itemType, stream.readInt());
itemPile.item.load(stream);
return itemPile;
}
use of io.xol.chunkstories.api.exceptions.UndefinedItemTypeException in project chunkstories-api by Hugobros3.
the class PacketInventoryMoveItemPile method process.
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
if (!(processor instanceof ServerPlayerPacketsProcessor)) {
processor.logger().warn("Received a " + this.getClass().getSimpleName() + " but this GameContext isn't providen with a packet processor made to deal with it");
return;
}
ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
Player player = sppc.getPlayer();
EntityControllable playerEntity = player.getControlledEntity();
oldX = in.readInt();
oldY = in.readInt();
newX = in.readInt();
newY = in.readInt();
amount = in.readInt();
from = InventoryTranslator.obtainInventoryHandle(in, processor);
to = InventoryTranslator.obtainInventoryHandle(in, processor);
// If this pile is spawned from the void
if (// || from == InventoryTranslator.INVENTORY_CREATIVE_TRASH)
from == null) {
try {
itemPile = ItemPile.obtainItemPileFromStream(player.getWorld().getContentTranslator(), in);
} catch (NullItemException e) {
// This ... isn't supposed to happen
processor.logger().info("User " + sender + " is trying to spawn a null ItemPile for some reason.");
} catch (UndefinedItemTypeException e) {
// This is slightly more problematic
processor.logger().warn(e.getMessage());
// e.printStackTrace(processor.getLogger().getPrintWriter());
}
} else {
itemPile = from.getItemPileAt(oldX, oldY);
}
// Check access
if (to != null && playerEntity != null) {
if (!to.isAccessibleTo(playerEntity)) {
player.sendMessage("You don't have access to this.");
return;
}
}
// Check using event
PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, itemPile, from, to, oldX, oldY, newX, newY, amount);
player.getContext().getPluginManager().fireEvent(moveItemEvent);
if (!moveItemEvent.isCancelled()) {
// Restrict item spawning
if (// || from instanceof InventoryLocalCreativeMenu)
from == null) {
if (player.hasPermission("items.spawn") || (player.getControlledEntity() != null && player.getControlledEntity() instanceof EntityCreative && ((EntityCreative) player.getControlledEntity()).getCreativeModeComponent().get())) {
// Let it happen when in creative mode or owns items.spawn perm
} else {
player.sendMessage("#C00000You are neither in creative mode nor have the items.spawn permission.");
return;
}
}
// If target inventory is null, this means the item was dropped
if (to == null) {
if (playerEntity == null) {
System.out.println("Dropping items isn't possible if the player doesn't control any entity.");
return;
}
// If we're pulling this out of an inventory ( and not /dev/null ), we need to remove it from that
Inventory sourceInventory = itemPile.getInventory();
Location loc = playerEntity.getLocation();
EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, itemPile);
player.getContext().getPluginManager().fireEvent(dropItemEvent);
if (!dropItemEvent.isCancelled()) {
if (sourceInventory != null)
sourceInventory.setItemPileAt(itemPile.getX(), itemPile.getY(), null);
if (dropItemEvent.getItemEntity() != null)
loc.getWorld().addEntity(dropItemEvent.getItemEntity());
}
return;
}
itemPile.moveItemPileTo(to, newX, newY, amount);
}
}
use of io.xol.chunkstories.api.exceptions.UndefinedItemTypeException in project chunkstories-core by Hugobros3.
the class EntityComponentSelectedItem method pull.
@Override
protected void pull(StreamSource from, DataInputStream dis) throws IOException {
selectedSlot = dis.readInt();
boolean itemIncluded = dis.readBoolean();
if (itemIncluded) {
// System.out.println("reading item from packet for entity"+entity);
// ItemPile pile;
ItemPile itemPile = null;
try {
itemPile = ItemPile.obtainItemPileFromStream(entity.getWorld().getContentTranslator(), dis);
} catch (NullItemException e) {
// Don't do anything about it, no big deal
} catch (UndefinedItemTypeException e) {
// This is slightly more problematic
// Logger logger = this.entity.getWorld().getGameContext().logger();
this.entity.getWorld().getGameContext().logger().info(e.getMessage());
// e.printStackTrace(logger.getPrintWriter());
}
// Ensures only client worlds accepts such pushes
if (!(entity.getWorld() instanceof WorldMaster))
inventory.setItemPileAt(selectedSlot, 0, itemPile);
/*int id = dis.readInt() & 0x00FFFFFF;
ItemDefinition itemType = ItemDefinitions.getItemDefinitionById(id);
if(itemType != null)
{
Item item = itemType.newItem();
pile = new ItemPile(item, dis);
if(pile != null && !(entity.getWorld() instanceof WorldMaster))
{
//System.out.println("got held item for "+entity + " : "+pile);
inventory.setItemPileAt(selectedSlot, 0, pile);
}
}*/
}
this.pushComponentEveryoneButController();
}
Aggregations