use of org.spongepowered.api.item.inventory.Container in project RedProtect by FabioZumbi12.
the class RPPlayerListener method onInteractBlock.
// listen all
@Listener(order = Order.FIRST, beforeModifications = true)
public void onInteractBlock(InteractBlockEvent event, @First Player p) {
RedProtect.get().logger.debug("player", "RPPlayerListener - InteractBlockEvent canceled? " + event.isCancelled());
BlockSnapshot b = event.getTargetBlock();
BlockState bstate = b.getState();
Location<World> l = null;
if (!b.getState().getType().equals(BlockTypes.AIR)) {
l = b.getLocation().get();
RedProtect.get().logger.debug("player", "RPPlayerListener - Is InteractBlockEvent event. The block is " + bstate.getType().getName());
} else {
l = p.getLocation();
}
Region r = RedProtect.get().rm.getTopRegion(l);
ItemType itemInHand = ItemTypes.NONE;
ItemStack stack = ItemStack.of(ItemTypes.NONE, 1);
if (p.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
stack = p.getItemInHand(HandTypes.MAIN_HAND).get();
itemInHand = stack.getItem();
if (RPUtil.removeGuiItem(stack)) {
p.setItemInHand(HandTypes.MAIN_HAND, ItemStack.of(ItemTypes.NONE, 1));
event.setCancelled(true);
}
} else if (p.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
stack = p.getItemInHand(HandTypes.OFF_HAND).get();
itemInHand = stack.getItem();
if (RPUtil.removeGuiItem(stack)) {
p.setItemInHand(HandTypes.OFF_HAND, ItemStack.of(ItemTypes.NONE, 1));
event.setCancelled(true);
}
}
if (itemInHand.getId().equalsIgnoreCase(RedProtect.get().cfgs.getString("wands.infoWandID"))) {
r = RedProtect.get().rm.getTopRegion(l);
if (RedProtect.get().ph.hasUserPerm(p, "RedProtect.get().infowand")) {
if (r == null) {
RPLang.sendMessage(p, "playerlistener.noregion.atblock");
} else if (r.canBuild(p)) {
p.sendMessage(RPUtil.toText(RPLang.get("general.color") + "--------------- [&e" + r.getName() + RPLang.get("general.color") + "] ---------------"));
p.sendMessage(r.info());
p.sendMessage(RPUtil.toText(RPLang.get("general.color") + "-----------------------------------------"));
} else {
p.sendMessage(RPUtil.toText(RPLang.get("playerlistener.region.entered").replace("{region}", r.getName()).replace("{leaders}", RPUtil.UUIDtoPlayer(r.getLeadersDesc()))));
}
event.setCancelled(true);
return;
}
}
// start player checks
if (r == null) {
if (b != null && (bstate instanceof Container || RedProtect.get().cfgs.getStringList("private.allowed-blocks").contains(bstate.getType().getName()))) {
Boolean out = RedProtect.get().cfgs.getBool("private.allow-outside");
if (out && !cont.canOpen(b, p)) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantopen");
event.setCancelled(true);
} else {
int x = b.getLocation().get().getBlockX();
int y = b.getLocation().get().getBlockY();
int z = b.getLocation().get().getBlockZ();
RPLang.sendMessage(p, RPLang.get("playerlistener.region.opened").replace("{region}", "X:" + x + " Y:" + y + " Z:" + z));
}
}
}
} else {
// if (r != null) && (b != null) >>
if (b != null) {
if (bstate.getType().equals(BlockTypes.DRAGON_EGG) || bstate.getType().equals(BlockTypes.BED) || bstate.getType().equals(BlockTypes.NOTEBLOCK) || bstate.getType().getName().contains("repeater") || bstate.getType().getName().contains("comparator")) {
if (!r.canBuild(p)) {
RPLang.sendMessage(p, "playerlistener.region.cantinteract");
event.setCancelled(true);
}
} else if ((b.getState() instanceof Carrier) || RedProtect.get().cfgs.getStringList("private.allowed-blocks").contains(bstate.getType().getName())) {
if ((r.canChest(p) && !cont.canOpen(b, p) || (!r.canChest(p) && cont.canOpen(b, p)) || (!r.canChest(p) && !cont.canOpen(b, p)))) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantopen");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.opened").replace("{region}", RPUtil.UUIDtoPlayer(r.getLeadersDesc())));
}
}
} else if (bstate.getType().getName().contains("lever")) {
if (!r.canLever(p)) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantlever");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.levertoggled").replace("{region}", RPUtil.UUIDtoPlayer(r.getLeadersDesc())));
}
}
} else if (bstate.getType().getName().contains("button")) {
if (!r.canButton(p)) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantbutton");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.buttonactivated").replace("{region}", RPUtil.UUIDtoPlayer(r.getLeadersDesc())));
}
}
} else if (RPDoor.isOpenable(b)) {
if (!r.canDoor(p)) /* || (r.canDoor(p) && !cont.canOpen(b, p))*/
{
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantdoor");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, "playerlistener.region.opendoor");
RPDoor.ChangeDoor(b, r);
}
} else {
RPDoor.ChangeDoor(b, r);
}
} else if (bstate.getType().getName().contains("rail")) {
if (!r.canMinecart(p)) {
RPLang.sendMessage(p, "blocklistener.region.cantplace");
event.setCancelled(true);
}
} else if (bstate.getType().getName().contains("sign") && !r.canSign(p)) {
if (b.get(Keys.SIGN_LINES).isPresent()) {
List<Text> sign = b.get(Keys.SIGN_LINES).get();
for (String tag : RedProtect.get().cfgs.getStringList("region-settings.allow-sign-interact-tags")) {
// check first rule
if (tag.equalsIgnoreCase(sign.get(0).toPlain())) {
return;
}
// check if tag is leaders or members names
if (tag.equalsIgnoreCase("{membername}")) {
for (String leader : r.getLeaders()) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(leader))) {
return;
}
}
for (String member : r.getMembers()) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(member))) {
return;
}
}
for (String admin : r.getAdmins()) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(admin))) {
return;
}
}
}
// check if tag is player name
if (tag.equalsIgnoreCase("{playername}")) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(p.getName()))) {
return;
}
}
}
}
RPLang.sendMessage(p, "playerlistener.region.cantinteract.signs");
event.setCancelled(true);
} else if ((itemInHand.equals(ItemTypes.FLINT_AND_STEEL) || itemInHand.equals(ItemTypes.WATER_BUCKET) || itemInHand.equals(ItemTypes.BUCKET) || itemInHand.equals(ItemTypes.LAVA_BUCKET) || itemInHand.equals(ItemTypes.ITEM_FRAME) || itemInHand.equals(ItemTypes.PAINTING)) && !r.canBuild(p)) {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.cantuse"));
event.setCancelled(true);
} else if (!r.allowMod(p) && !RPUtil.isBukkitBlock(bstate)) {
RPLang.sendMessage(p, "playerlistener.region.cantinteract");
event.setCancelled(true);
}
}
}
}
use of org.spongepowered.api.item.inventory.Container in project Nucleus by NucleusPowered.
the class KitEditCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
if (KIT_HANDLER.isOpen(kitInfo.getName())) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.edit.current", kitInfo.getName()));
}
Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.edit.title", kitInfo.getName()))).build(plugin);
kitInfo.getStacks().stream().filter(x -> !x.getType().equals(ItemTypes.NONE)).forEach(x -> inventory.offer(x.createStack()));
Optional<Container> openedInventory = src.openInventory(inventory);
if (openedInventory.isPresent()) {
KIT_HANDLER.addKitInventoryToListener(Tuple.of(kitInfo, inventory), openedInventory.get());
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.kit.edit.cantopen", kitInfo.getName());
}
use of org.spongepowered.api.item.inventory.Container in project Nucleus by NucleusPowered.
the class InvSeeCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
User target = args.<User>getOne(player).get();
if (!target.isOnline() && !permissions.testSuffix(src, "offline")) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.invsee.nooffline"));
}
if (!this.self && target.getUniqueId().equals(src.getUniqueId())) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.invsee.self"));
}
if (permissions.testSuffix(target, "exempt.target", src, false)) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.invsee.targetexempt", target.getName()));
}
// Just in case, get the subject inventory if they are online.
try {
Inventory targetInv = target.isOnline() ? target.getPlayer().get().getInventory() : target.getInventory();
Optional<Container> oc = src.openInventory(targetInv);
if (oc.isPresent()) {
if (!permissions.testSuffix(src, "modify") || permissions.testSuffix(target, "exempt.interact")) {
InvSeeListener.addEntry(src.getUniqueId(), oc.get());
}
return CommandResult.success();
}
throw ReturnMessageException.fromKey("command.invsee.failed");
} catch (UnsupportedOperationException e) {
throw ReturnMessageException.fromKey("command.invsee.offlinenotsupported");
}
}
use of org.spongepowered.api.item.inventory.Container in project SpongeCommon by SpongePowered.
the class ContainerUtil method generateLens.
/**
* Generates a fallback lens for given Container
*
* @param container The Container to generate a lens for
* @param slots The slots of the Container
* @return The generated fallback lens
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Lens<IInventory, ItemStack> generateLens(net.minecraft.inventory.Container container, SlotProvider<IInventory, ItemStack> slots) {
// Get all inventories viewed in the Container & count slots & retain order
Map<Optional<IInventory>, List<Slot>> viewed = container.inventorySlots.stream().collect(Collectors.groupingBy(i -> Optional.<IInventory>ofNullable(i.inventory), LinkedHashMap::new, Collectors.toList()));
// Count the index
int index = 0;
CraftingInventoryData crafting = new CraftingInventoryData();
List<Lens<IInventory, ItemStack>> lenses = new ArrayList<>();
for (Map.Entry<Optional<IInventory>, List<Slot>> entry : viewed.entrySet()) {
List<Slot> slotList = entry.getValue();
int slotCount = slotList.size();
IInventory subInventory = entry.getKey().orElse(null);
// Generate Lens based on existing InventoryAdapter
Lens<IInventory, ItemStack> lens = generateAdapterLens(slots, index, crafting, slotList, subInventory);
// Check if sub-inventory is LensProvider
if (lens == null && subInventory instanceof LensProvider) {
Fabric<IInventory> keyFabric = MinecraftFabric.of(subInventory);
lens = ((LensProvider) subInventory).rootLens(keyFabric, new VanillaAdapter(keyFabric, container));
}
// Unknown Inventory or Inventory size <> Lens size
if (lens == null || lens.slotCount() != slotCount) {
if (subInventory instanceof InventoryCraftResult) {
// InventoryCraftResult is a Slot
Slot slot = slotList.get(0);
lens = new CraftingOutputSlotLensImpl(index, item -> slot.isItemValid(((ItemStack) item)), itemType -> (slot.isItemValid((ItemStack) org.spongepowered.api.item.inventory.ItemStack.of(itemType, 1))));
} else if (subInventory instanceof InventoryCrafting) {
// InventoryCrafting has width and height and is Input
InventoryCrafting craftGrid = (InventoryCrafting) subInventory;
lens = new GridInventoryLensImpl(index, craftGrid.getWidth(), craftGrid.getHeight(), craftGrid.getWidth(), InputSlot.class, slots);
} else if (slotCount == 1) {
// Unknown - A single Slot
lens = new SlotLensImpl(index);
} else if (subInventory instanceof InventoryBasic && subInventory.getClass().isAnonymousClass()) {
// Anonymous InventoryBasic -> Check for Vanilla Containers:
switch(subInventory.getName()) {
// Container InputSlots
case "Enchant":
case // Container InputSlots
"Repair":
lens = new OrderedInventoryLensImpl(index, slotCount, 1, InputSlot.class, slots);
break;
default:
// Unknown
lens = new OrderedInventoryLensImpl(index, slotCount, 1, slots);
}
} else {
// Unknown - fallback to OrderedInventory
lens = new OrderedInventoryLensImpl(index, slotCount, 1, slots);
}
}
lenses.add(lens);
index += slotCount;
}
List<Lens<IInventory, ItemStack>> additional = new ArrayList<>();
try {
if (crafting.out != null && crafting.base != null && crafting.grid != null) {
additional.add(new CraftingInventoryLensImpl(crafting.out, crafting.base, crafting.grid.getWidth(), crafting.grid.getHeight(), slots));
}
} catch (Exception e) {
SpongeImpl.getLogger().error("Error while creating CraftingInventoryLensImpl for " + container.getClass().getName(), e);
}
// Lens containing/delegating to other lenses
return new ContainerLens((InventoryAdapter<IInventory, ItemStack>) container, slots, lenses, additional);
}
use of org.spongepowered.api.item.inventory.Container in project modules-extra by CubeEngine.
the class BackpackInventory method closeInventory.
public void closeInventory() {
viewers.stream().map(id -> Sponge.getServer().getPlayer(id)).forEach(p -> p.ifPresent(Player::closeInventory));
for (Player player : ((Container) view.getInventory()).getViewers()) {
player.closeInventory();
}
this.saveData(view.getInventory());
}
Aggregations