Skip to content

Commit

Permalink
Merge pull request #165 from SpaceServerUniverse/dev/syoyu-autocollect
Browse files Browse the repository at this point in the history
Dev/syoyu autocollect
  • Loading branch information
OneWalkDev authored Oct 22, 2024
2 parents b8e48f4 + 047ef42 commit cb9f034
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Bukkit;
import space.yurisi.universecorev2.UniverseCoreV2;
import space.yurisi.universecorev2.subplugins.autocollect.AutoCollect;
import space.yurisi.universecorev2.subplugins.birthdaycard.BirthdayCard;
import space.yurisi.universecorev2.subplugins.blockcopystick.BlockCopyStick;
import space.yurisi.universecorev2.subplugins.changemessages.ChangeMessages;
Expand Down Expand Up @@ -84,6 +85,7 @@ private void registerPlugin() {
this.subPlugins.add(new ReceiveBox());
this.subPlugins.add(new Elevator());
this.subPlugins.add(new BirthdayCard());
this.subPlugins.add(new AutoCollect());
}

public void onEnable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package space.yurisi.universecorev2.subplugins.autocollect;

import org.bukkit.Bukkit;
import space.yurisi.universecorev2.UniverseCoreV2;
import space.yurisi.universecorev2.subplugins.SubPlugin;
import space.yurisi.universecorev2.subplugins.autocollect.command.AutoCollectCommand;
import space.yurisi.universecorev2.subplugins.autocollect.data.AutoCollectMap;
import space.yurisi.universecorev2.subplugins.autocollect.event.AutoCollectListener;

public class AutoCollect implements SubPlugin {

@Override
public void onEnable(UniverseCoreV2 core) {
new AutoCollectMap();
Bukkit.getPluginManager().registerEvents(new AutoCollectListener(), core);
core.getCommand("autocollect").setExecutor(new AutoCollectCommand());
}

@Override
public void onDisable() {
//NOOP
}

@Override
public String getName() {
return "AutoCollect";
}

@Override
public String getVersion() {
return "1.0.0";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package space.yurisi.universecorev2.subplugins.autocollect.command;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import space.yurisi.universecorev2.subplugins.autocollect.data.AutoCollectMap;
import space.yurisi.universecorev2.utils.Message;

public class AutoCollectCommand implements CommandExecutor {

public static final String ACCommand = "[収集AI]";

@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if(commandSender instanceof Player player) {
AutoCollectMap.getInstance().toggleAutoCollect(player);
Message.sendSuccessMessage(player, ACCommand, "§a自動収集モードを[ §c§l"+(AutoCollectMap.getInstance().isAutoCollect(player) ? "ON":"OFF")+" §a]に変更しました");
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package space.yurisi.universecorev2.subplugins.autocollect.data;

import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

public class AutoCollectMap {

private static AutoCollectMap instance;
private final Map<String, Boolean> autoCollectMap;

public AutoCollectMap() {
instance = this;
this.autoCollectMap = new HashMap<>();
}

public static AutoCollectMap getInstance() {
return instance;
}

public void toggleAutoCollect(Player player){
this.autoCollectMap.put(player.getUniqueId().toString(),!this.autoCollectMap.get(player.getUniqueId().toString()));
}

public boolean isAutoCollect(Player player){
return this.autoCollectMap.get(player.getUniqueId().toString());
}

public void register(Player player){
this.autoCollectMap.put(player.getUniqueId().toString(),false);
}

public boolean isRegistered(Player player){
return this.autoCollectMap.containsKey(player.getUniqueId().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package space.yurisi.universecorev2.subplugins.autocollect.event;

import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import space.yurisi.universecorev2.subplugins.autocollect.command.AutoCollectCommand;
import space.yurisi.universecorev2.subplugins.autocollect.data.AutoCollectMap;
import space.yurisi.universecorev2.utils.Message;

import java.util.List;
import java.util.Objects;

public class AutoCollectListener implements Listener{

@EventHandler
public void onDrop(BlockDropItemEvent event) {
Player player = event.getPlayer();
if(!AutoCollectMap.getInstance().isAutoCollect(player)) return;
List<Item> itemEntity = event.getItems();
outside:for(int i = 0; i < itemEntity.size(); i++){
ItemStack dropItem = itemEntity.get(i).getItemStack();
for (int j = 0; j < player.getInventory().getStorageContents().length; j++) {
ItemStack item = player.getInventory().getItem(j);
if(item == null){
player.getInventory().addItem(dropItem);
itemEntity.remove(i);
continue outside;
}
if(Objects.equals(item.getType(), dropItem.getType())){
int dropAmount = dropItem.getAmount();
int maxStackSize = item.getMaxStackSize();
int currentItemAmount = item.getAmount();
if(!Objects.equals(item.getItemMeta(), dropItem.getItemMeta())) continue;
if(maxStackSize - currentItemAmount < dropAmount) continue;
player.getInventory().addItem(dropItem);
itemEntity.remove(i);
continue outside;
}
}
}
boolean isDrop = false;
for (Item dropItem : itemEntity) {
if (dropItem == null) continue;
isDrop = true;
ItemStack item = dropItem.getItemStack();
player.getWorld().dropItem(player.getLocation(), item);
}
if(isDrop){
Message.sendWarningMessage(player, AutoCollectCommand.ACCommand, "インベントリが満杯です!");
}
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
if(AutoCollectMap.getInstance().isRegistered(event.getPlayer())) return;
AutoCollectMap.getInstance().register(event.getPlayer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.scoreboard.Scoreboard;
import space.yurisi.universecorev2.exception.MoneyNotFoundException;
import space.yurisi.universecorev2.exception.UserNotFoundException;
import space.yurisi.universecorev2.subplugins.autocollect.data.AutoCollectMap;
import space.yurisi.universecorev2.subplugins.universeeconomy.UniverseEconomyAPI;

import java.time.LocalTime;
Expand Down Expand Up @@ -42,7 +43,10 @@ public void run() {
Objects.requireNonNull(objective).getScore("§r ").setScore(-5);
setOnline(objective, -6);
setJob(objective, -7);
setSpaceShipPoint(objective, -8);
if(AutoCollectMap.getInstance().isAutoCollect(player)) {
Objects.requireNonNull(objective).getScore("§a自動収集モード").setScore(-8);
}
setSpaceShipPoint(objective, -9);
player.setScoreboard(scoreboard);
changeTick();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ commands:
item:
description: メインハンドに持ったアイテムの名前を表示します

## AutoCollect
autocollect:
description: 自動収集機能のオンオフを切り替えます
aliases: [ ac ]

# ReceiveBox
receive:
description: アイテム受取ボックスを表示します
Expand Down

0 comments on commit cb9f034

Please sign in to comment.