Skip to content

Commit

Permalink
QuickHomes v1
Browse files Browse the repository at this point in the history
  • Loading branch information
shinixsensei committed Sep 11, 2021
1 parent 8303c72 commit 39d1a52
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 11 deletions.
21 changes: 19 additions & 2 deletions out/production/RIN/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
name: RIN
main: dev.angelsflyinhell.rin.RIN
author: angelsflyinhell
version: 1.0.0
api-version: 1.14
version: 1.1.0
api-version: 1.14
commands:
sethome:
description: Save your current location as a Home Point.
usage: /<command> <name>
aliases: [ sh, seth, shome ]
delhome:
description: Delete a Home Point from your Home Point list.
usage: /<command> <name>
aliases: [ dh, del0h, dhome ]
homes:
description: View a list of all Home Points owned by you.
usage: /<command>
aliases: [hs, hlist, homel]
home:
description: Select a Home Point to teleport to.
usage: /<command> <name>
aliases: [ h, hp, tpH, teleportH, tpHome ]
25 changes: 21 additions & 4 deletions src/dev/angelsflyinhell/rin/RIN.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package dev.angelsflyinhell.rin;

import dev.angelsflyinhell.rin.events.BetterSaplings;
import dev.angelsflyinhell.rin.plugins.BetterSaplings;
import dev.angelsflyinhell.rin.plugins.QuickHomes;
import dev.angelsflyinhell.rin.tools.ConsoleUtils;
import dev.angelsflyinhell.rin.tools.RINConfig;
import dev.angelsflyinhell.rin.tools.props.QHSave;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Objects;

public class RIN extends JavaPlugin {

@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new BetterSaplings(), this);
getServer().getConsoleSender().sendMessage( ChatColor.DARK_PURPLE + ConsoleUtils.PREFIX + "Loaded " + ConsoleUtils.FEATURES);
RINConfig.init();

if(!RINConfig.propExist("bettersaplings") || Objects.equals(RINConfig.getValue("bettersaplings"), "true")) {
getServer().getPluginManager().registerEvents(new BetterSaplings(), this);
}

if(!RINConfig.propExist("quickhomes") || Objects.equals(RINConfig.getValue("quickhomes"), "true")) {
QHSave.init();
Objects.requireNonNull(getCommand("sethome")).setExecutor(new QuickHomes());
Objects.requireNonNull(getCommand("delhome")).setExecutor(new QuickHomes());
Objects.requireNonNull(getCommand("homes")).setExecutor(new QuickHomes());
Objects.requireNonNull(getCommand("home")).setExecutor(new QuickHomes());
}
getServer().getConsoleSender().sendMessage( ChatColor.DARK_PURPLE + ConsoleUtils.PREFIX + "Plugin loaded!");
}

@Override
public void onDisable() {
getServer().getConsoleSender().sendMessage(ChatColor.DARK_PURPLE + ConsoleUtils.PREFIX + "Unloaded " + ConsoleUtils.FEATURES);
getServer().getConsoleSender().sendMessage(ChatColor.DARK_PURPLE + ConsoleUtils.PREFIX + "Plugin unloaded");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.angelsflyinhell.rin.events;
package dev.angelsflyinhell.rin.plugins;

import org.bukkit.Location;
import org.bukkit.event.EventHandler;
Expand Down
82 changes: 82 additions & 0 deletions src/dev/angelsflyinhell/rin/plugins/QuickHomes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package dev.angelsflyinhell.rin.plugins;

import dev.angelsflyinhell.rin.tools.ConsoleUtils;
import dev.angelsflyinhell.rin.tools.RINConfig;
import dev.angelsflyinhell.rin.tools.props.QHSave;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Objects;

public class QuickHomes implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player))
return true;

Player player = (Player) sender;

if (command.getName().equalsIgnoreCase("sethome")) {

if (args[0].equals("")) {
player.sendMessage(ConsoleUtils.PREFIX + "Home Point cannot be empty.");
}

int playerHomes = 0;
for (int i = 0; i < QHSave.getKeySize(); i++) {
String key = QHSave.properties.keySet().toArray()[i].toString();
if(key.startsWith(String.valueOf(player.getUniqueId()))) {
playerHomes++;
}
}

if (RINConfig.propExist("quickhomesLimit") && Integer.parseInt(RINConfig.getValue("quickhomesLimit"))<=playerHomes) {
player.sendMessage(ConsoleUtils.PREFIX + "Home Point couldn't be set: Exceeds limit of " + RINConfig.getValue("quickhomesLimit"));
return true;
}

String key = player.getUniqueId() + "home" + args[0];
String loc = player.getLocation().getX() + ";" + player.getLocation().getY() + ";" + player.getLocation().getZ() + ";" + player.getWorld().getName();
QHSave.addKey(key, loc);
player.sendMessage(ConsoleUtils.PREFIX + "Set Home Point: " + args[0]);
}

if (command.getName().equalsIgnoreCase("delhomes")) {
String key = player.getUniqueId() + "home" + args[0];
if (QHSave.propExist(key)) {
QHSave.addKey(key, "DELETED");
player.sendMessage(ConsoleUtils.PREFIX + "Deleted Home Point: " + args[0]);
}
player.sendMessage(ConsoleUtils.PREFIX + "Couldn't find Home Point \"" + args[0] + "\"");
}

if (command.getName().equalsIgnoreCase("homes")) {
player.sendMessage(ConsoleUtils.PREFIX + "Your Homes: ");
for (int i = 0; i < QHSave.getKeySize(); i++) {
String key = QHSave.properties.keySet().toArray()[i].toString();
if (key.startsWith(String.valueOf(player.getUniqueId()))) {
String outKey = key.replace(player.getUniqueId() + "home", "");
player.sendMessage(ConsoleUtils.PREFIX + (i + 1) + ". " + outKey);
}
}
}

if (command.getName().equalsIgnoreCase("home")) {
String key = player.getUniqueId() + "home" + args[0];
if (!QHSave.propExist(key) || Objects.equals(QHSave.getValue(key), "DELETED")) {
player.sendMessage(ConsoleUtils.PREFIX + "Couldn't find Home Point \"" + args[0] + "\"");
return true;
}

String[] s = QHSave.getValue(key).split(";");
Location loc = new Location(Bukkit.getWorld(s[3]), Double.parseDouble(s[0]), Double.parseDouble(s[1]), Double.parseDouble(s[2]));
player.teleport(loc);
}

return true;
}
}
4 changes: 2 additions & 2 deletions src/dev/angelsflyinhell/rin/tools/ConsoleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public class ConsoleUtils {

public static String VERSION = "1.0.0";
public static String VERSION = "1.1.0";
public static String PREFIX = "[RIN v" + VERSION + "] ";
public static String FEATURES = "[BetterSaplings v1]";
public static String FEATURES = "[BetterSaplings v1, QuickHomes v1]";

}
58 changes: 58 additions & 0 deletions src/dev/angelsflyinhell/rin/tools/RINConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.angelsflyinhell.rin.tools;

import java.io.*;
import java.util.Properties;

public class RINConfig {
private static Properties properties;
static File file = new File("config.rin");

public static void init(){
properties = new Properties();
try {
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
fileNotFoundAction(file);
}
}

public static boolean propExist(String key) {
if (properties.getProperty(key) == null) {
return false;
} else {
return true;
}
}

public static String getValue(String key) {
return properties.getProperty(key);
}

public static void addKey(String hash, String name) {
properties.put(hash, name);
try {
properties.store(new FileOutputStream(file), "RIN Configuration");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void deleteKey(String hash) {
properties.remove(hash);
}

private static void fileNotFoundAction(File f){
try {
properties.store(new FileOutputStream(f), "RIN Configuration");
} catch (IOException e) {
e.printStackTrace();
}
init();
}

public static int getKeySize() {
return properties.size();
}

}
58 changes: 58 additions & 0 deletions src/dev/angelsflyinhell/rin/tools/props/QHSave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.angelsflyinhell.rin.tools.props;

import java.io.*;
import java.util.Properties;

public class QHSave {

public static Properties properties;
static File file = new File("quickhomes.rin");

public static void init(){
properties = new Properties();
try {
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
fileNotFoundAction(file);
}
}

public static boolean propExist(String key) {
if (properties.getProperty(key) == null) {
return false;
} else {
return true;
}
}

public static String getValue(String key) {
return properties.getProperty(key);
}

public static void addKey(String hash, String name) {
properties.put(hash, name);
try {
properties.store(new FileOutputStream(file), "quickhomes savefile");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void deleteKey(String hash) {
properties.remove(hash);
}

private static void fileNotFoundAction(File f){
try {
properties.store(new FileOutputStream(f), "quickhomes savefile");
} catch (IOException e) {
e.printStackTrace();
}
init();
}

public static int getKeySize() {
return properties.size();
}
}
21 changes: 19 additions & 2 deletions src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
name: RIN
main: dev.angelsflyinhell.rin.RIN
author: angelsflyinhell
version: 1.0.0
api-version: 1.14
version: 1.1.0
api-version: 1.14
commands:
sethome:
description: Save your current location as a Home Point.
usage: /<command> <name>
aliases: [ sh, seth, shome ]
delhome:
description: Delete a Home Point from your Home Point list.
usage: /<command> <name>
aliases: [ dh, del0h, dhome ]
homes:
description: View a list of all Home Points owned by you.
usage: /<command>
aliases: [hs, hlist, homel]
home:
description: Select a Home Point to teleport to.
usage: /<command> <name>
aliases: [ h, hp, tpH, teleportH, tpHome ]

0 comments on commit 39d1a52

Please sign in to comment.