Skip to content

Commit

Permalink
1.2 - The teleportation update!
Browse files Browse the repository at this point in the history
- Added entire Teleportation system, that contains registration, settings, visual and default settings, to-and-from configuration file, teleport cooldown, custom tasks and much more!
- Added Util.java, and it's main method Util.getOrDefault,
- Added OpSerializableLocation.java which can be serialized inside a serializable class, therefore OpLocation.java should not be used as a primary class location now,
- Added OpTimerRunnable.java which contains async and not async methods counting down and up,
- Added PermissionGroup.java and PermissionManager.java that saved permissions and reads them from configuration file.
  • Loading branch information
ThisKarolGajda committed Aug 23, 2022
1 parent d68fda0 commit ce92669
Show file tree
Hide file tree
Showing 26 changed files with 981 additions and 23 deletions.
8 changes: 8 additions & 0 deletions src/main/java/me/opkarol/opc/OpC.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package me.opkarol.opc;

import me.opkarol.opc.api.commands.OpCommand;
import me.opkarol.opc.api.language.LanguageDatabase;
import me.opkarol.opc.api.plugin.OpPlugin;
import me.opkarol.opc.api.teleport.OpTeleport;
import me.opkarol.opc.api.teleport.TeleportRegistration;

public final class OpC extends OpPlugin {
private LanguageDatabase langDb;
Expand All @@ -13,6 +16,11 @@ public void onEnable() {
// OpPluginLanguage language = LanguageDatabase.addPluginLanguage(new OpPluginLanguage(getInstance(), LanguageType.en_US, LanguageType.pl_PL));
super.onEnable();

OpTeleport teleport = new OpTeleport(15, "world", 2370, -1780).setRegistration(new TeleportRegistration());
teleport.saveInConfiguration(this.getConfiguration(), "teleport-test-1");
new OpCommand("test")
.executeAsPlayer((sender, args) -> teleport.teleportCopied(sender))
.register();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import me.opkarol.opc.api.map.OpLinkedMap;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class MySqlTable {
private final OpLinkedMap<MySqlVariable, MySqlAttribute[]> map = new OpLinkedMap<>();
private String tableName;
Expand All @@ -27,7 +29,7 @@ public MySqlTable addMySqlVariable(String name, MySqlVariableType type, MySqlAtt
}

public String toCreateTableString() {
return finalString == null ? String.format("CREATE TABLE IF NOT EXISTS %s (%s);", getTableName(), getTableValues()) : finalString;
return getOrDefault(finalString, String.format("CREATE TABLE IF NOT EXISTS %s (%s);", getTableName(), getTableValues()));
}

public String getTableValues() {
Expand Down Expand Up @@ -60,7 +62,7 @@ public String getTableValues() {
}

public String getTableName() {
return tableName == null ? "DEFAULT_NULL_TABLE" : String.format("`%s`", tableName);
return getOrDefault(String.format("`%s`", tableName), "DEFAULT_NULL_TABLE");
}

public void setTableName(String tableName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.util.function.BiFunction;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class PlaceholderAPI {
private static PlaceholderAPI placeholderAPI;
private final boolean enabled;
Expand All @@ -18,7 +20,7 @@ public PlaceholderAPI() {
}

public static PlaceholderAPI getInstance() {
return placeholderAPI == null ? new PlaceholderAPI() : placeholderAPI;
return getOrDefault(placeholderAPI, new PlaceholderAPI());
}

public boolean isEnabled() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/me/opkarol/opc/api/extensions/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class Vault {
private static Vault vault;
private boolean enabled;
Expand All @@ -19,7 +21,7 @@ public Vault() {
}

public static Vault getInstance() {
return vault == null ? new Vault() : vault;
return getOrDefault(vault, new Vault());
}

public boolean isEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Optional;
import java.util.UUID;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class OpInventoryCache {
private static OpInventoryCache cache;
private final OpMap<UUID, OpInventoryObject> lastInventories = new OpMap<>();
Expand All @@ -16,7 +18,7 @@ public OpInventoryCache() {
}

public static OpInventoryCache getCache() {
return cache == null ? new OpInventoryCache() : cache;
return getOrDefault(cache, new OpInventoryCache());
}

public void setLastInventory(UUID uuid, OpInventoryObject builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class OpInventoryBuilder {
private String inventoryName;
private OpInventoryRows rows;
Expand Down Expand Up @@ -134,7 +136,7 @@ public OpInventoryBuilder addPlayers(@NotNull List<Player> players) {
}

public Inventory getLastInventory() {
return lastInventory == null ? build() : lastInventory;
return getOrDefault(lastInventory, build());
}

public OpInventoryBuilder setLastInventory(Inventory lastInventory) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/me/opkarol/opc/api/head/HeadDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.util.Optional;

public class HeadDatabase {
import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class HeadDatabase{
private static HeadDatabase headDatabase;
private final OpMap<String, ItemStack> map = new OpMap<>();

Expand All @@ -17,7 +19,7 @@ public HeadDatabase() {
}

public static HeadDatabase getInstance() {
return headDatabase == null ? new HeadDatabase() : headDatabase;
return getOrDefault(headDatabase, new HeadDatabase());
}

public ItemStack getHead(String owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.List;
import java.util.UUID;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class LanguageDatabase {
private static LanguageDatabase languageDatabase;
private static final List<OpPluginLanguage> languages = new ArrayList<>();
Expand Down Expand Up @@ -41,7 +43,7 @@ public static synchronized OpPluginLanguage addPluginLanguage(OpPluginLanguage l
}

public static LanguageDatabase getInstance() {
return languageDatabase == null ? new LanguageDatabase() : languageDatabase;
return getOrDefault(languageDatabase, new LanguageDatabase());
}

public List<OpPluginLanguage> getLanguages() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/me/opkarol/opc/api/map/OpLinkedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
= Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import me.opkarol.opc.api.utils.Util;

import java.io.Serializable;
import java.util.*;

Expand Down Expand Up @@ -92,7 +94,7 @@ public V getOrDefault(K key, V defaultValue) {
return defaultValue;
}
V get = getMap().get(key);
return get == null ? defaultValue : get;
return Util.getOrDefault(get, defaultValue);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/me/opkarol/opc/api/map/OpMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
= Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import me.opkarol.opc.api.utils.Util;

import java.io.Serializable;
import java.util.*;

Expand Down Expand Up @@ -92,7 +94,7 @@ public V getOrDefault(K key, V defaultValue) {
return defaultValue;
}
V get = getMap().get(key);
return get == null ? defaultValue : get;
return Util.getOrDefault(get, defaultValue);
}

@Override
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/me/opkarol/opc/api/misc/OpLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public OpLocation(String string) {
world = null;
}

public OpLocation(@NotNull OpSerializableLocation location) {
this(location.toString());
}

public double getX() {
return x;
}
Expand Down Expand Up @@ -122,11 +126,25 @@ public String toString() {
return String.format("%s;%s;%s;%s;%s;%s", getStringX(), getStringY(), getStringZ(), getStringPitch(), getStringYaw(), getStringWorld());
}

public String toFamilyString() {
return String.format("X: %s Y: %s Z: %s World: %s", getStringX(), getStringY(), getStringZ(), getStringWorld());
}

public OpLocation getLastLocation() {
return lastLocation;
}

public void setLastLocation() {
this.lastLocation = new OpLocation(toString());
}

public OpSerializableLocation toLocation() {
return new OpSerializableLocation(toString());
}

public OpSerializableLocation getHighestYLocation() {
Location location = getLocation();
location.setY(getWorld().getHighestBlockYAt((int) getX(), (int) getZ()));
return new OpSerializableLocation(location);
}
}
15 changes: 10 additions & 5 deletions src/main/java/me/opkarol/opc/api/misc/OpParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.ArrayList;
import java.util.List;

import static me.opkarol.opc.api.utils.Util.getOrDefault;

public class OpParticle implements Serializable {
private float offsetX, offsetY, offsetZ;
private int amount;
Expand Down Expand Up @@ -162,7 +164,7 @@ public OpParticle setReceivers(List<Player> receivers) {
}

public OpParticle addReceiver(Player player) {
List<Player> list = receivers == null ? new ArrayList<>() : receivers;
List<Player> list = getOrDefault(receivers, new ArrayList<>());
list.add(player);
return setReceivers(list);
}
Expand All @@ -180,16 +182,19 @@ public <T> OpParticle display(List<Player> players, T specialData) {
return this;
}

players.forEach(player -> player.spawnParticle(particle, location.getX(), location.getY(), location.getZ(), amount, offsetX, offsetY, offsetZ, specialData));
if (location != null) {
players.forEach(player -> player.spawnParticle(particle, location.getX(), location.getY(), location.getZ(), amount, offsetX, offsetY, offsetZ, specialData));
}
return this;
}

public OpParticle display(Player player) {
if (player == null) {
return this;
}

player.spawnParticle(particle, location.getX(), location.getY(), location.getZ(), amount, offsetX, offsetY, offsetZ);
if (location != null) {
player.spawnParticle(particle, location.getX(), location.getY(), location.getZ(), amount, offsetX, offsetY, offsetZ);
}
return this;
}

Expand All @@ -198,7 +203,7 @@ public OpParticle display(List<Player> players) {
return this;
}

players.forEach(player -> player.spawnParticle(particle, location.getX(), location.getY(), location.getZ(), amount, offsetX, offsetY, offsetZ));
players.forEach(this::display);
return this;
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/me/opkarol/opc/api/misc/OpRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.opkarol.opc.OpAPI;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
Expand All @@ -26,14 +27,17 @@ public OpRunnable set(Consumer<OpRunnable> consumer) {
return this;
}

public static OpRunnable get(Consumer<OpRunnable> consumer) {
@Contract("_ -> new")
public static @NotNull OpRunnable get(Consumer<OpRunnable> consumer) {
return new OpRunnable(consumer);
}

public void cancel() {
public boolean cancelTask() {
if (task != null && !task.isCancelled()) {
task.cancel();
return true;
}
return false;
}

@NotNull
Expand Down
Loading

0 comments on commit ce92669

Please sign in to comment.