Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjananas committed Feb 15, 2022
0 parents commit a7b55da
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Ninjananas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# No Creeper Grief

Spigot plugin that contains in-game utilities related to chests.

## Features

### Autostore
A command that lets you store your inventory in the surrounding chests containing similar items. It is inspired by the similar functionnality in Terraria. Chests that cannot be accessed by the player are ignored.

### Autosort [planned]
Automatically sort your inventory or a chest.

## Commands
- `/autostore` Store (and sort) items in your inventory to nearby chests.
13 changes: 13 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: ChestTricks
main: ninjananas.mc.ChestTricks.Plugin
version: 1.0
api-version: 1.18
permissions:
chesttricks.autostore:
default: true
commands:
autostore:
description: Automatically store items in your inventory to nearby chests
usage: /autostore
permission: chesttricks.autostore
permission-message: You can't use this command!
76 changes: 76 additions & 0 deletions src/ninjananas/mc/ChestTricks/AutoStoreCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ninjananas.mc.ChestTricks;


import java.util.Vector;

import org.bukkit.FluidCollisionMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.RayTraceResult;

public class AutoStoreCommand implements CommandExecutor {

public AutoStoreCommand(Plugin plugin) {
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String val, String[] args) {
if (! (sender instanceof Player)) {
sender.sendMessage("This command can only be used by players!");
return false;
}
Player player = (Player) sender;
Location location = player.getEyeLocation();

Vector<Chest> chests = new Vector<Chest>();

for (double i = -3.0; i < 4.0; i++) {
for (double j = -3.0; j < 4.0; j++) {
for (double k = -3.0; k < 4.0; k++) {
Block block = location.clone().add(i, j, k).getBlock();
if (block.getType() == Material.CHEST) {
RayTraceResult rayTraceResult = player.getWorld().rayTraceBlocks(
location,
block.getLocation().clone().add(0.5, 0.5, 0.5).subtract(location).toVector(),
20,
FluidCollisionMode.NEVER,
false);
if ( (rayTraceResult != null) & (rayTraceResult.getHitBlock().equals(block)) )
chests.add((Chest) block.getState());
}
}
}
}

for (Chest chest: chests) {
Inventory chestInventory = chest.getInventory();
Inventory playerInventory = player.getInventory();

ItemStack[] inventoryContent = playerInventory.getStorageContents().clone();

for (int i = 0; i < inventoryContent.length; i++) {
if (i < 9) continue; // Skip elements in action bar
ItemStack stack = inventoryContent[i];
if (stack == null) continue; // Skip empty slots
if (! chestInventory.contains(stack.getType())) continue; // Only store items that are in the chest

inventoryContent[i] = null;
for (ItemStack failedStack: chestInventory.addItem(stack).values())
inventoryContent[i] = failedStack;
}
playerInventory.setStorageContents(inventoryContent);
player.updateInventory();
}

return true;
}

}
16 changes: 16 additions & 0 deletions src/ninjananas/mc/ChestTricks/Plugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ninjananas.mc.ChestTricks;

import org.bukkit.plugin.java.JavaPlugin;


public class Plugin extends JavaPlugin {

@Override
public void onEnable() {
this.getCommand("autostore").setExecutor(new AutoStoreCommand(this));
}

@Override
public void onDisable() {
}
}

0 comments on commit a7b55da

Please sign in to comment.