Skip to content

Commit

Permalink
Version 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
poqdavid committed Mar 26, 2022
1 parent e73c35b commit 0e09275
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 221 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ m_description=A simple backpack plugin

## Version
m_major=1
m_minor=3
m_minor=4
m_api=S7.4
m_suffix=STABLE

Expand All @@ -28,8 +28,8 @@ spongemixin=0.8.5-SNAPSHOT
minecraft_version=1.12.2
forge_rundir=run/forge
forgeVersion=1.12.2-14.23.5.2860
mappings_channel=snapshot
mappings_version=20180814-1.12
mappings_channel=stable
mappings_version=39-1.12

bstats=2.2.1
validation=1.1.0.Final
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package io.github.poqdavid.nyx.nyxbackpack.Commands;

import io.github.poqdavid.nyx.nyxbackpack.NyxBackpack;
import io.github.poqdavid.nyx.nyxbackpack.Utils.Backpack;
import io.github.poqdavid.nyx.nyxcore.Permissions.BackpackPermission;
import io.github.poqdavid.nyx.nyxcore.Utils.CoreTools;
import org.spongepowered.api.command.CommandException;
Expand All @@ -31,10 +30,7 @@
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.property.InventoryTitle;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.serializer.TextSerializers;

import java.io.File;
import java.nio.file.Files;
Expand Down Expand Up @@ -65,9 +61,7 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
if (player_cmd_src.hasPermission(BackpackPermission.COMMAND_BACKPACK_MAIN)) {

this.backpackCheckLock(player_cmd_src, player_cmd_src);

final Backpack backpack = new Backpack(player_cmd_src, player_cmd_src, this.getBackpackSize(player_cmd_src), true, nb);
player_cmd_src.openInventory(backpack.getBackpack());
NyxBackpack.Backpacks.get(player_cmd_src.getUniqueId()).openBackpack();

} else {
throw new CommandPermissionException(Text.of("You don't have permission to use this command."));
Expand All @@ -78,77 +72,12 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
return CommandResult.success();
}

public int getBackpackSize(Player player) {
if (player.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_SIX))
return 6;
if (player.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_FIVE))
return 5;
if (player.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_FOUR))
return 4;
if (player.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_THREE))
return 3;
if (player.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_TWO))
return 2;
if (player.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_ONE))
return 1;
return 1;
}


private void backpackCheckLock(Player player, Player playerSrc) throws CommandException {

Path file = Paths.get(this.nb.getConfigPath() + File.separator + "backpacks" + File.separator + player.getUniqueId() + ".lock");

if (Files.exists(file)) {
throw new CommandPermissionException(Text.of("Sorry currently your backpack is locked."));
} else {

if (isBackpackOpen(player)) {
throw new CommandPermissionException(Text.of("Sorry currently your backpack is locked!!"));
}
}
}

private Boolean isBackpackOpen(Player player) {
String tl = player.getName() + "'s " + "Backpack";
if (player.isOnline()) {
if (player.isViewingInventory()) {
Inventory inv = player.getInventory();

InventoryTitle title = inv.getInventoryProperty(InventoryTitle.class).orElse(InventoryTitle.of(Text.of("NONE")));
String titles = TextSerializers.FORMATTING_CODE.serialize(title.getValue());

if (titles.equals("Backpack")) {
return true;
} else {
return searchInvs(tl);
}

} else {

return searchInvs(tl);
}

} else {
return searchInvs(tl);
}
}

private Boolean searchInvs(String title) {
for (Player pl : this.nb.getGame().getServer().getOnlinePlayers()) {
if (pl.isViewingInventory()) {
Inventory inv2 = pl.getInventory();

InventoryTitle title2 = inv2.getInventoryProperty(InventoryTitle.class).orElse(InventoryTitle.of(Text.of("NONE")));
String titles2 = TextSerializers.FORMATTING_CODE.serialize(title2.getValue());

if (titles2.equals(title)) {
return true;
}
}
}
return false;
}


}
126 changes: 22 additions & 104 deletions src/main/java/io/github/poqdavid/nyx/nyxbackpack/Commands/OpenCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.property.InventoryTitle;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.serializer.TextSerializers;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -71,8 +68,6 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
user_args = user.get();
}

final Integer bpszie = args.<Integer>getOne("size").orElse(0);

src.sendMessage(Text.of("§6Opening Backpack for:"));

src.sendMessage(Text.of("§6Name: §7" + user_args.getName()));
Expand All @@ -84,18 +79,31 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
if (user_args.hasPermission(BackpackPermission.COMMAND_BACKPACK_MAIN)) {
if (args.hasAny("m")) {
if (player_cmd_src.hasPermission(BackpackPermission.COMMAND_BACKPACK_ADMIN_MODIFY)) {
this.backpackcheck(user_args);
this.backpackchecklock(user_args, player_cmd_src);

final Backpack backpack = new Backpack(user_args, player_cmd_src, this.getBackpackSize(user_args, bpszie), true, this.nb);
player_cmd_src.openInventory(backpack.getBackpack());
this.backpackCheck(user_args);
this.backpackCheckLock(user_args, player_cmd_src);

if (!NyxBackpack.Backpacks.containsKey(user_args.getUniqueId())) {
Backpack backpack = new Backpack(user_args, CoreTools.getBackpackSize(user_args));
NyxBackpack.Backpacks.put(user_args.getUniqueId(), backpack);
}

NyxBackpack.Backpacks.get(user_args.getUniqueId()).openBackpack(player_cmd_src, false, false);

} else {
throw new CommandPermissionException(Text.of("You don't have permission to modify other backpacks."));
}
} else {
this.backpackcheck(user_args);
final Backpack backpack = new Backpack(user_args, player_cmd_src, this.getBackpackSize(user_args, bpszie), false, nb);
player_cmd_src.openInventory(backpack.getBackpack());

this.backpackCheck(user_args);

if (!NyxBackpack.Backpacks.containsKey(user_args.getUniqueId())) {
Backpack backpack = new Backpack(user_args, CoreTools.getBackpackSize(user_args));
NyxBackpack.Backpacks.put(user_args.getUniqueId(), backpack);
}

NyxBackpack.Backpacks.get(user_args.getUniqueId()).openBackpack(player_cmd_src, true);

}
} else {
throw new CommandPermissionException(Text.of("This user doesn't have permission to use backpack."));
Expand All @@ -116,106 +124,16 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
return CommandResult.success();
}

public int getBackpackSize(User user) {
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_SIX))
return 6;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_FIVE))
return 5;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_FOUR))
return 4;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_THREE))
return 3;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_TWO))
return 2;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_ONE))
return 1;
return 1;
}

public int getBackpackSize(User user, Integer size) {
if (size == 0) {
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_SIX))
return 6;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_FIVE))
return 5;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_FOUR))
return 4;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_THREE))
return 3;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_TWO))
return 2;
if (user.hasPermission(BackpackPermission.COMMAND_BACKPACK_SIZE_ONE))
return 1;
return 1;
} else {
if (size < 6) {
return 6;
} else {
return size;
}
}

}

private void backpackchecklock(User user, Player playersrc) throws CommandException {
private void backpackCheckLock(User user, Player playersrc) throws CommandException {

Path file = Paths.get(this.nb.getConfigPath() + File.separator + "backpacks" + File.separator + user.getUniqueId() + ".lock");

if (Files.exists(file)) {
throw new CommandPermissionException(Text.of("Sorry currently your backpack is locked."));
} else {
if (isBackpackOpen(user)) {
throw new CommandPermissionException(Text.of("Sorry currently your backpack is locked!!"));
}
}
}

private Boolean isBackpackOpen(User user) {
String tl = user.getName() + "'s " + "Backpack";
if (user.isOnline()) {
if (user.getPlayer().isPresent()) {
Player player = user.getPlayer().get();
if (player.isViewingInventory()) {
Inventory inv = player.getInventory();

InventoryTitle title = inv.getInventoryProperty(InventoryTitle.class).orElse(InventoryTitle.of(Text.of("NONE")));
String titles = TextSerializers.FORMATTING_CODE.serialize(title.getValue());

if (titles == "Backpack") {
return true;
} else {
return searchInvs(tl);
}

} else {

return searchInvs(tl);
}
} else {
return searchInvs(tl);
}
} else {
return searchInvs(tl);
}
}

private Boolean searchInvs(String title) {
for (Player pl : this.nb.getGame().getServer().getOnlinePlayers()) {
if (pl.isViewingInventory()) {
Inventory inv2 = pl.getInventory();

InventoryTitle title2 = inv2.getInventoryProperty(InventoryTitle.class).orElse(InventoryTitle.of(Text.of("NONE")));
String titles2 = TextSerializers.FORMATTING_CODE.serialize(title2.getValue());

if (titles2 == title) {
return true;
}
}
}
return false;
}

private void backpackcheck(User user) throws CommandException {
private void backpackCheck(User user) throws CommandException {
Path file = Paths.get(this.nb.getConfigPath() + File.separator + "backpacks" + File.separator + user.getUniqueId() + ".backpack");
if (!Files.exists(file)) {
throw new CommandPermissionException(Text.of("Sorry there is no backpack data for " + user.getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of NyxBackpack.
*
* NyxBackpack 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.
*
* NyxBackpack 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 NyxBackpack. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) POQDavid <https://github.com/poqdavid/NyxBackpack>
* Copyright (c) contributors
*/

package io.github.poqdavid.nyx.nyxbackpack.Listeners;

import io.github.poqdavid.nyx.nyxbackpack.NyxBackpack;
import io.github.poqdavid.nyx.nyxbackpack.Utils.Backpack;
import io.github.poqdavid.nyx.nyxcore.Utils.CoreTools;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.network.ClientConnectionEvent;

import java.util.Optional;
import java.util.UUID;

public class NyxBackpackListener {

@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event) {
Optional<Player> optionalPlayer = CoreTools.getPlayer(event.getCause());
if (optionalPlayer.isPresent()) {
Player player = optionalPlayer.get();
CoreTools.MakeNewBP(player);

if (!NyxBackpack.Backpacks.containsKey(player.getUniqueId())) {
Backpack backpack = new Backpack(player, CoreTools.getBackpackSize(player));
NyxBackpack.Backpacks.put(player.getUniqueId(), backpack);
}
}
}

@Listener
public void onPlayerDisconnect(ClientConnectionEvent.Disconnect event) {
Optional<Player> optionalPlayer = CoreTools.getPlayer(event.getCause());

if (optionalPlayer.isPresent()) {
final UUID uuid = optionalPlayer.get().getUniqueId();

if (NyxBackpack.Backpacks.containsKey(uuid)) {
if (NyxBackpack.Backpacks.get(uuid).readOnly.isEmpty()) {
NyxBackpack.Backpacks.remove(uuid);
}
}
}
}

}
Loading

0 comments on commit 0e09275

Please sign in to comment.