-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from SpaceServerUniverse/dev/syoyu-autocollect
Dev/syoyu autocollect
- Loading branch information
Showing
7 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/space/yurisi/universecorev2/subplugins/autocollect/AutoCollect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...n/java/space/yurisi/universecorev2/subplugins/autocollect/command/AutoCollectCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/space/yurisi/universecorev2/subplugins/autocollect/data/AutoCollectMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...in/java/space/yurisi/universecorev2/subplugins/autocollect/event/AutoCollectListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters