fix give and remove items
This commit is contained in:
parent
3ab007cc1e
commit
f03714c95c
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
0
.idea/.gitignore
generated
vendored
Normal file → Executable file
0
.idea/.gitignore
generated
vendored
Normal file → Executable file
3
.idea/gradle.xml
generated
Normal file → Executable file
3
.idea/gradle.xml
generated
Normal file → Executable file
@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
@ -7,7 +8,7 @@
|
|||||||
<option name="gradleHome" value="" />
|
<option name="gradleHome" value="" />
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="/mnt/work/source/ItemEconomy" />
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
|
2
.idea/misc.xml
generated
Normal file → Executable file
2
.idea/misc.xml
generated
Normal file → Executable file
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="temurin-21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
0
.idea/uiDesigner.xml
generated
Normal file → Executable file
0
.idea/uiDesigner.xml
generated
Normal file → Executable file
2
.idea/vcs.xml
generated
Normal file → Executable file
2
.idea/vcs.xml
generated
Normal file → Executable file
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
<mapping directory="" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
2
build.gradle
Normal file → Executable file
2
build.gradle
Normal file → Executable file
@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'ru.themixray.itemeconomy'
|
group = 'ru.themixray.itemeconomy'
|
||||||
version = '0.1.0'
|
version = '0.1.1'
|
||||||
|
|
||||||
spigot {
|
spigot {
|
||||||
desc {
|
desc {
|
||||||
|
0
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file → Executable file
0
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file → Executable file
0
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file → Executable file
0
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file → Executable file
0
gradlew.bat
vendored
Normal file → Executable file
0
gradlew.bat
vendored
Normal file → Executable file
0
settings.gradle
Normal file → Executable file
0
settings.gradle
Normal file → Executable file
0
src/main/java/ru/themixray/itemeconomy/Config.java
Normal file → Executable file
0
src/main/java/ru/themixray/itemeconomy/Config.java
Normal file → Executable file
66
src/main/java/ru/themixray/itemeconomy/Main.java
Normal file → Executable file
66
src/main/java/ru/themixray/itemeconomy/Main.java
Normal file → Executable file
@ -8,18 +8,33 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.plugin.ServicePriority;
|
import org.bukkit.plugin.ServicePriority;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class Main extends JavaPlugin {
|
public class Main extends JavaPlugin {
|
||||||
|
public static HashMap<String, List<ItemStack>> addItems = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml"));
|
Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml"));
|
||||||
Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High);
|
Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High);
|
||||||
|
new BukkitRunnable() {
|
||||||
|
public void run() {
|
||||||
|
new HashMap<>(addItems).forEach((k, v) -> {
|
||||||
|
Player player = Bukkit.getPlayerExact(k);
|
||||||
|
|
||||||
|
if (player != null) {
|
||||||
|
for (ItemStack item : v) {
|
||||||
|
addItems(player, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
addItems.remove(k);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}.runTaskTimer(this, 20, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -31,14 +46,43 @@ public class Main extends JavaPlugin {
|
|||||||
return Arrays.stream(player.getInventory().getContents()).map(o -> o == null ? new ItemStack(Material.AIR) : o).toList();
|
return Arrays.stream(player.getInventory().getContents()).map(o -> o == null ? new ItemStack(Material.AIR) : o).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean removeItems(Player player, Material type, int amount) {
|
public static boolean removeItems(String playerName, Material type, int amount) {
|
||||||
if(player.getInventory().all(type).values().stream().mapToInt(ItemStack::getAmount).sum()<amount)return false;
|
System.out.println("try remove item "+type+" "+amount+" from "+playerName);
|
||||||
player.getInventory().removeItem(new ItemStack(type,amount));
|
ItemStack item = new ItemStack(type, amount);
|
||||||
return true;
|
|
||||||
|
Player player = Bukkit.getPlayerExact(playerName);
|
||||||
|
|
||||||
|
if (player == null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return removeItems(player, item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addItems(Player player, Material type, int amount) {
|
public static void addItems(String playerName, Material type, int amount) {
|
||||||
HashMap<Integer, ItemStack> nope=player.getInventory().addItem(new ItemStack(type,amount));
|
System.out.println("try add item "+type+" "+amount+" to "+playerName);
|
||||||
for(ItemStack v:nope.values())player.getWorld().dropItemNaturally(player.getLocation(),v);
|
ItemStack item = new ItemStack(type, amount);
|
||||||
|
|
||||||
|
Player player = Bukkit.getPlayerExact(playerName);
|
||||||
|
|
||||||
|
if (player == null) {
|
||||||
|
List<ItemStack> items = addItems.getOrDefault(playerName, new ArrayList<>());
|
||||||
|
items.add(item);
|
||||||
|
addItems.put(playerName, items);
|
||||||
|
} else {
|
||||||
|
addItems(player, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean removeItems(Player player, ItemStack item) {
|
||||||
|
System.out.println("remove item "+item.getType()+" "+item.getAmount()+" from "+player.getName());
|
||||||
|
if (player.getInventory().all(item.getType()).values().stream().mapToInt(ItemStack::getAmount).sum() < item.getAmount()) return false;
|
||||||
|
return player.getInventory().removeItem(item).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addItems(Player player, ItemStack item) {
|
||||||
|
System.out.println("add item "+item.getType()+" "+item.getAmount()+" from "+player.getName());
|
||||||
|
HashMap<Integer, ItemStack> nope = player.getInventory().addItem(item);
|
||||||
|
for (ItemStack v:nope.values()) player.getWorld().dropItemNaturally(player.getLocation(), v);
|
||||||
}
|
}
|
||||||
}
|
}
|
22
src/main/java/ru/themixray/itemeconomy/VaultLayer.java
Normal file → Executable file
22
src/main/java/ru/themixray/itemeconomy/VaultLayer.java
Normal file → Executable file
@ -83,16 +83,7 @@ public class VaultLayer implements Economy {
|
|||||||
EconomyResponse.ResponseType.FAILURE,
|
EconomyResponse.ResponseType.FAILURE,
|
||||||
"Недостаточно средств");
|
"Недостаточно средств");
|
||||||
|
|
||||||
Player player;
|
if (!Main.removeItems(playerName, Config.ITEM, (int) Math.ceil(amount))) {
|
||||||
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
|
||||||
return new EconomyResponse(
|
|
||||||
amount,
|
|
||||||
getBalance(playerName),
|
|
||||||
EconomyResponse.ResponseType.FAILURE,
|
|
||||||
"Игрок офлайн");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Main.removeItems(player, Config.ITEM, (int) amount)) {
|
|
||||||
return new EconomyResponse(
|
return new EconomyResponse(
|
||||||
amount,
|
amount,
|
||||||
getBalance(playerName),
|
getBalance(playerName),
|
||||||
@ -112,16 +103,7 @@ public class VaultLayer implements Economy {
|
|||||||
if (amount == 0) return new EconomyResponse(amount,getBalance(playerName),EconomyResponse.ResponseType.SUCCESS,null);
|
if (amount == 0) return new EconomyResponse(amount,getBalance(playerName),EconomyResponse.ResponseType.SUCCESS,null);
|
||||||
if (amount < 0) return withdrawPlayer(playerName,-amount);
|
if (amount < 0) return withdrawPlayer(playerName,-amount);
|
||||||
|
|
||||||
Player player;
|
Main.addItems(playerName, Config.ITEM, (int) amount);
|
||||||
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
|
||||||
return new EconomyResponse(
|
|
||||||
amount,
|
|
||||||
getBalance(playerName),
|
|
||||||
EconomyResponse.ResponseType.FAILURE,
|
|
||||||
"Игрок офлайн");
|
|
||||||
}
|
|
||||||
|
|
||||||
Main.addItems(player, Config.ITEM, (int) amount);
|
|
||||||
|
|
||||||
return new EconomyResponse(
|
return new EconomyResponse(
|
||||||
amount,
|
amount,
|
||||||
|
0
src/main/resources/config.yml
Normal file → Executable file
0
src/main/resources/config.yml
Normal file → Executable file
Loading…
Reference in New Issue
Block a user