use of org.bukkit.inventory.PlayerInventory in project Prism-Bukkit by prism.
the class WandCommand method handle.
/**
* Handle the command
*/
@Override
public void handle(CallInfo call) {
String type = "i";
final boolean isInspect = call.getArg(0).equalsIgnoreCase("inspect") || call.getArg(0).equalsIgnoreCase("i");
if (!isInspect) {
if (call.getArgs().length < 2) {
call.getPlayer().sendMessage(Prism.messenger.playerError("You need to specify a wand type. Use '/prism ?' for help."));
return;
}
type = call.getArg(1);
}
Wand oldwand = null;
if (Prism.playersWithActiveTools.containsKey(call.getPlayer().getName())) {
// Pull the wand in use
oldwand = Prism.playersWithActiveTools.get(call.getPlayer().getName());
}
// Always remove the old one
Prism.playersWithActiveTools.remove(call.getPlayer().getName());
// Determine default mode
String mode = plugin.getConfig().getString("prism.wands.default-mode");
// Check if the player has a personal override
if (plugin.getConfig().getBoolean("prism.wands.allow-user-override")) {
final String personalMode = Settings.getSetting("wand.mode", call.getPlayer());
if (personalMode != null) {
mode = personalMode;
}
}
// Determine which item we're using.
int item_id = 0;
byte item_subid = -1;
String toolKey = null;
if (mode.equals("item")) {
toolKey = plugin.getConfig().getString("prism.wands.default-item-mode-id");
} else if (mode.equals("block")) {
toolKey = plugin.getConfig().getString("prism.wands.default-block-mode-id");
}
// Check if the player has a personal override
if (plugin.getConfig().getBoolean("prism.wands.allow-user-override")) {
final String personalToolKey = Settings.getSetting("wand.item", call.getPlayer());
if (personalToolKey != null) {
toolKey = personalToolKey;
}
}
if (toolKey != null) {
if (!toolKey.contains(":")) {
toolKey += ":0";
}
final String[] toolKeys = toolKey.split(":");
item_id = Integer.parseInt(toolKeys[0]);
item_subid = Byte.parseByte(toolKeys[1]);
}
String wandOn = "";
String item_name = "";
String parameters = "";
if (item_id != 0) {
item_name = Prism.getItems().getAlias(item_id, item_subid);
wandOn += " on a " + item_name;
}
for (int i = (isInspect ? 1 : 2); i < call.getArgs().length; i++) {
if (parameters.isEmpty()) {
parameters += " using:" + ChatColor.GRAY;
}
parameters += " " + call.getArg(i);
}
if (!ItemUtils.isAcceptableWand(item_id, item_subid)) {
call.getPlayer().sendMessage(Prism.messenger.playerError("Sorry, but you may not use " + item_name + " for a wand."));
return;
}
boolean enabled = false;
Wand wand = null;
/**
* Inspector wand
*/
if (type.equalsIgnoreCase("i") || type.equalsIgnoreCase("inspect")) {
if (!call.getPlayer().hasPermission("prism.lookup") && !call.getPlayer().hasPermission("prism.wand.inspect")) {
call.getPlayer().sendMessage(Prism.messenger.playerError("You do not have permission for this."));
return;
}
if (oldwand != null && oldwand instanceof InspectorWand) {
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Inspection wand " + ChatColor.RED + "disabled" + ChatColor.WHITE + "."));
} else {
wand = new InspectorWand(plugin);
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Inspection wand " + ChatColor.GREEN + "enabled" + ChatColor.WHITE + wandOn + parameters + "."));
enabled = true;
}
} else /**
* Profile wand
*/
if (type.equalsIgnoreCase("p") || type.equalsIgnoreCase("profile")) {
if (!call.getPlayer().hasPermission("prism.lookup") && !call.getPlayer().hasPermission("prism.wand.profile")) {
call.getPlayer().sendMessage(Prism.messenger.playerError("You do not have permission for this."));
return;
}
if (oldwand != null && oldwand instanceof ProfileWand) {
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Profile wand " + ChatColor.RED + "disabled" + ChatColor.WHITE + "."));
} else {
wand = new ProfileWand();
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Profile wand " + ChatColor.GREEN + "enabled" + ChatColor.WHITE + wandOn + "."));
enabled = true;
}
} else /**
* Rollback wand
*/
if (type.equalsIgnoreCase("rollback") || type.equalsIgnoreCase("rb")) {
if (!call.getPlayer().hasPermission("prism.rollback") && !call.getPlayer().hasPermission("prism.wand.rollback")) {
call.getPlayer().sendMessage(Prism.messenger.playerError("You do not have permission for this."));
return;
}
if (oldwand != null && oldwand instanceof RollbackWand) {
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Rollback wand " + ChatColor.RED + "disabled" + ChatColor.WHITE + "."));
} else {
wand = new RollbackWand(plugin);
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Rollback wand " + ChatColor.GREEN + "enabled" + ChatColor.WHITE + wandOn + parameters + "."));
enabled = true;
}
} else /**
* Restore wand
*/
if (type.equalsIgnoreCase("restore") || type.equalsIgnoreCase("rs")) {
if (!call.getPlayer().hasPermission("prism.restore") && !call.getPlayer().hasPermission("prism.wand.restore")) {
call.getPlayer().sendMessage(Prism.messenger.playerError("You do not have permission for this."));
return;
}
// If disabling this one
if (oldwand != null && oldwand instanceof RestoreWand) {
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Restore wand " + ChatColor.RED + "disabled" + ChatColor.WHITE + "."));
} else {
wand = new RestoreWand(plugin);
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Restore wand " + ChatColor.GREEN + "enabled" + ChatColor.WHITE + wandOn + parameters + "."));
enabled = true;
}
} else /**
* Off
*/
if (type.equalsIgnoreCase("off")) {
call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Current wand " + ChatColor.RED + "disabled" + ChatColor.WHITE + "."));
} else // Not a valid wand
{
call.getPlayer().sendMessage(Prism.messenger.playerError("Invalid wand type. Use /prism ? for help."));
return;
}
final PlayerInventory inv = call.getPlayer().getInventory();
if (enabled) {
wand.setWandMode(mode);
wand.setItemId(item_id);
wand.setItemSubId(item_subid);
Prism.debug("Wand activated for player - mode: " + mode + " Item:" + item_id + ":" + item_subid);
// Move any existing item to the hand, otherwise give it to them
if (plugin.getConfig().getBoolean("prism.wands.auto-equip")) {
if (!InventoryUtils.moveItemToHand(inv, item_id, item_subid)) {
// Store the item they're holding, if any
wand.setOriginallyHeldItem(inv.getItemInHand());
// They don't have the item, so we need to give them an item
if (InventoryUtils.handItemToPlayer(inv, new ItemStack(item_id, 1, item_subid))) {
wand.setItemWasGiven(true);
} else {
call.getPlayer().sendMessage(Prism.messenger.playerError("Can't fit the wand item into your inventory."));
}
}
call.getPlayer().updateInventory();
}
// Let's build the QueryParameters for it if it's a Query wand.
if (wand instanceof QueryWandBase) {
if (!((QueryWandBase) wand).setParameters(call.getPlayer(), call.getArgs(), (isInspect ? 1 : 2))) {
// This
// returns
// if
// it
// was
// successful
call.getPlayer().sendMessage(Prism.messenger.playerError("Notice: Only some parameters used.."));
}
}
// Store
Prism.playersWithActiveTools.put(call.getPlayer().getName(), wand);
} else {
if (oldwand != null) {
oldwand.disable(call.getPlayer());
}
}
}
use of org.bukkit.inventory.PlayerInventory in project xAuth by CypherX.
the class PlayerDataHandler method restoreData.
public void restoreData(xAuthPlayer xp, Player p) {
ItemStack[] items = null;
ItemStack[] armor = null;
Location loc = null;
Collection<PotionEffect> potFx = null;
int fireTicks = 0;
int remainingAir = 300;
// Use cached copy of player data, if it exists
PlayerData playerData = xp.getPlayerData();
if (playerData != null) {
items = playerData.getItems();
armor = playerData.getArmor();
loc = playerData.getLocation();
potFx = playerData.getPotionEffects();
fireTicks = playerData.getFireTicks();
remainingAir = playerData.getRemainingAir();
} else {
Connection conn = plugin.getDbCtrl().getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = String.format("SELECT `items`, `armor`, `location`, `potioneffects`, `fireticks`, `remainingair` FROM `%s` WHERE `playername` = ?", plugin.getDbCtrl().getTable(Table.PLAYERDATA));
ps = conn.prepareStatement(sql);
ps.setString(1, p.getName());
rs = ps.executeQuery();
if (rs.next()) {
items = buildItemStack(rs.getString("items"));
armor = buildItemStack(rs.getString("armor"));
String rsLoc = rs.getString("location");
if (rsLoc != null) {
String[] locSplit = rsLoc.split(":");
loc = new Location(Bukkit.getWorld(locSplit[0]), Double.parseDouble(locSplit[1]), Double.parseDouble(locSplit[2]), Double.parseDouble(locSplit[3]), Float.parseFloat(locSplit[4]), Float.parseFloat(locSplit[5]));
}
String rsPotFx = rs.getString("potioneffects");
if (rsPotFx != null)
potFx = buildPotFx(rsPotFx);
fireTicks = rs.getInt("fireticks");
remainingAir = rs.getInt("remainingair");
}
} catch (SQLException e) {
xAuthLog.severe("Failed to load playerdata from database for player: " + p.getName(), e);
} finally {
plugin.getDbCtrl().close(conn, ps, rs);
}
}
PlayerInventory pInv = p.getInventory();
boolean hideInv = plugin.getConfig().getBoolean("guest.hide-inventory");
boolean hideLoc = plugin.getConfig().getBoolean("guest.protect-location");
if (hideInv && items != null) {
// Fix for inventory extension plugins
if (pInv.getSize() > items.length) {
ItemStack[] newItems = new ItemStack[pInv.getSize()];
for (int i = 0; i < items.length; i++) newItems[i] = items[i];
items = newItems;
}
//End Fix for inventory extension plugins
pInv.setContents(items);
}
if (hideInv && armor != null)
pInv.setArmorContents(armor);
if (hideLoc && loc != null && p.getHealth() > 0)
p.teleport(loc);
if (potFx != null)
p.addPotionEffects(potFx);
p.setFireTicks(fireTicks);
p.setRemainingAir(remainingAir);
xp.setPlayerData(null);
Connection conn = plugin.getDbCtrl().getConnection();
PreparedStatement ps = null;
try {
String sql = String.format("DELETE FROM `%s` WHERE `playername` = ?", plugin.getDbCtrl().getTable(Table.PLAYERDATA));
ps = conn.prepareStatement(sql);
ps.setString(1, p.getName());
ps.executeUpdate();
} catch (SQLException e) {
xAuthLog.severe("Could not delete playerdata record from database for player: " + p.getName(), e);
} finally {
plugin.getDbCtrl().close(conn, ps);
}
}
use of org.bukkit.inventory.PlayerInventory in project xAuth by CypherX.
the class PlayerDataHandler method storeData.
public void storeData(xAuthPlayer xp, Player p) {
PlayerInventory pInv = p.getInventory();
ItemStack[] items = pInv.getContents();
ItemStack[] armor = pInv.getArmorContents();
Location loc = p.isDead() ? null : p.getLocation();
Collection<PotionEffect> potEffects = p.getActivePotionEffects();
int fireTicks = p.isDead() ? 0 : p.getFireTicks();
int remainingAir = p.getRemainingAir();
String strItems = null;
String strArmor = null;
String strLoc = null;
String strPotFx = buildPotFxString(potEffects);
xp.setPlayerData(new PlayerData(items, armor, loc, potEffects, fireTicks, remainingAir));
boolean hideInv = plugin.getConfig().getBoolean("guest.hide-inventory");
boolean hideLoc = plugin.getConfig().getBoolean("guest.protect-location");
if (hideInv) {
strItems = buildItemString(items);
strArmor = buildItemString(armor);
pInv.clear();
pInv.setHelmet(null);
pInv.setChestplate(null);
pInv.setLeggings(null);
pInv.setBoots(null);
}
if (hideLoc && !p.isDead()) {
strLoc = loc.getWorld().getName() + ":" + loc.getX() + ":" + loc.getY() + ":" + loc.getZ() + ":" + loc.getYaw() + ":" + loc.getPitch();
p.teleport(plugin.getLocMngr().getLocation(p.getWorld()));
}
for (PotionEffect effect : p.getActivePotionEffects()) p.addPotionEffect(new PotionEffect(effect.getType(), 0, 0), true);
p.setFireTicks(0);
p.setRemainingAir(300);
// only store player data if there's something to insert
if (strItems != null || strArmor != null || strLoc != null || strPotFx != null) {
Connection conn = plugin.getDbCtrl().getConnection();
PreparedStatement ps = null;
try {
String sql;
if (plugin.getDbCtrl().isMySQL())
sql = String.format("INSERT IGNORE INTO `%s` VALUES (?, ?, ?, ?, ?, ?, ?)", plugin.getDbCtrl().getTable(Table.PLAYERDATA));
else
sql = String.format("INSERT INTO `%s` SELECT ?, ?, ?, ?, ?, ?, ? FROM DUAL WHERE NOT EXISTS (SELECT * FROM `%s` WHERE `playername` = ?)", plugin.getDbCtrl().getTable(Table.PLAYERDATA), plugin.getDbCtrl().getTable(Table.PLAYERDATA));
ps = conn.prepareStatement(sql);
ps.setString(1, p.getName());
ps.setString(2, strItems);
ps.setString(3, strArmor);
ps.setString(4, strLoc);
ps.setString(5, strPotFx);
ps.setInt(6, fireTicks);
ps.setInt(7, remainingAir);
if (!plugin.getDbCtrl().isMySQL())
ps.setString(8, p.getName());
ps.executeUpdate();
} catch (SQLException e) {
xAuthLog.severe("Failed to insert player data into database!", e);
} finally {
plugin.getDbCtrl().close(conn, ps);
}
}
}
use of org.bukkit.inventory.PlayerInventory in project Minigames by AddstarMC.
the class PlayerHasItemCondition method check.
private boolean check(MinigamePlayer player) {
PositionType checkType = PositionType.valueOf(where.getFlag().toUpperCase());
if (checkType == null) {
checkType = PositionType.ANYWHERE;
}
PlayerInventory inventory = player.getPlayer().getInventory();
ItemStack[] searchItems;
int startSlot;
int endSlot;
if (checkType == PositionType.ARMOR) {
searchItems = inventory.getArmorContents();
startSlot = 0;
endSlot = searchItems.length;
} else {
searchItems = inventory.getContents();
if (checkType == PositionType.HOTBAR) {
startSlot = 0;
endSlot = 9;
} else if (checkType == PositionType.MAIN) {
startSlot = 9;
endSlot = 36;
} else if (checkType == PositionType.SLOT) {
startSlot = slot.getFlag();
endSlot = startSlot + 1;
} else {
startSlot = 0;
endSlot = searchItems.length;
}
}
Material material = Material.getMaterial(type.getFlag());
Pattern namePattern = null;
Pattern lorePattern = null;
if (matchName.getFlag()) {
namePattern = createNamePattern();
}
if (matchLore.getFlag()) {
lorePattern = createLorePattern();
}
for (int i = startSlot; i < endSlot && i < searchItems.length; ++i) {
ItemStack itemInSlot = searchItems[i];
if (itemInSlot == null) {
continue;
}
if (itemInSlot.getType() == material) {
if (useData.getFlag() && itemInSlot.getDurability() != data.getFlag()) {
continue;
}
ItemMeta meta = itemInSlot.getItemMeta();
if (matchName.getFlag()) {
Matcher m = namePattern.matcher(meta.getDisplayName());
if (!m.matches()) {
continue;
}
}
if (matchLore.getFlag()) {
if (meta.getLore() != null) {
Matcher m = lorePattern.matcher(Joiner.on('\n').join(meta.getLore()));
if (!m.matches()) {
continue;
}
} else {
// Only an unset lore pattern can match this
if (lore.getFlag() != null) {
continue;
}
}
}
// This item completely matches
return true;
}
}
return false;
}
use of org.bukkit.inventory.PlayerInventory in project BKCommonLib by bergerhealer.
the class MapSession method update.
/**
* Updates the session. Returns false if the session should be ended.
*
* @return False when the session should be ended
*/
public boolean update() {
MapDisplayInfo info = this.display.getMapInfo();
if (info == null) {
// can't keep a session around for a map that does not exist!
return false;
}
// Update players and their input
this.hasHolders = false;
this.hasViewers = false;
this.hasNewViewers = false;
if (!this.onlineOwners.isEmpty()) {
Iterator<Owner> onlineIter = this.onlineOwners.iterator();
while (onlineIter.hasNext()) {
Owner owner = onlineIter.next();
// Update input
owner.input.doTick(this.display, owner.interceptInput);
// Check if online. Remove offline players if not set FOREVER
if (!owner.player.isOnline()) {
if (this.mode != MapSessionMode.FOREVER) {
this.owners.remove(owner.player.getUniqueId());
}
onOwnerRemoved(owner);
onlineIter.remove();
continue;
}
// Check if holding the map
PlayerInventory inv = owner.player.getInventory();
owner.controlling = info.isMap(inv.getItemInMainHand());
owner.holding = owner.controlling || info.isMap(inv.getItemInOffHand());
if (!owner.holding && this.mode == MapSessionMode.HOLDING) {
this.owners.remove(owner.player.getUniqueId());
onOwnerRemoved(owner);
onlineIter.remove();
continue;
}
// Check if viewing the map at all
owner.wasViewing = owner.viewing;
owner.viewing = owner.holding || info.frameViewers.contains(owner.player);
if (!owner.viewing && this.mode == MapSessionMode.VIEWING) {
this.owners.remove(owner.player.getUniqueId());
onOwnerRemoved(owner);
onlineIter.remove();
continue;
}
// Update state
this.hasHolders |= owner.holding;
this.hasViewers |= owner.viewing;
this.hasNewViewers |= owner.isNewViewer();
}
}
// Session end condition
switch(this.mode) {
case FOREVER:
return true;
case ONLINE:
return !onlineOwners.isEmpty();
case VIEWING:
return this.hasViewers;
case HOLDING:
return this.hasHolders;
}
return true;
}
Aggregations