Skip to content

Commit

Permalink
Moves DisplayOperator and DisplayOperatorReader from BlobDesign to Bl…
Browse files Browse the repository at this point in the history
…obLib

ObjectManager now implements CommandTarget for their asset
  • Loading branch information
anjoismysign committed Mar 22, 2024
1 parent e5885c0 commit 0d1d0ea
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/main/java/us/mytheria/bloblib/entities/ObjectManager.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package us.mytheria.bloblib.entities;

import me.anjoismysign.anjo.entities.Result;
import me.anjoismysign.skeramidcommands.command.CommandTarget;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.entities.logger.BlobPluginLogger;
import us.mytheria.bloblib.managers.Manager;
import us.mytheria.bloblib.managers.ManagerDirector;

import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
Expand All @@ -22,7 +20,8 @@
* @author An ObjectManager will handle objects that are loaded in
* random access memory and tracked by a key.
*/
public abstract class ObjectManager<T extends BlobObject> extends Manager {
public abstract class ObjectManager<T extends BlobObject> extends Manager
implements CommandTarget<T> {
private final File loadFilesDirectory;
private final Supplier<Map<String, T>> objectsSupplier;
private final Supplier<Map<String, File>> fileSupplier;
Expand Down Expand Up @@ -223,4 +222,13 @@ public void whenFilesLoad(Consumer<ObjectManager<T>> consumer) {
consumer.accept(this);
});
}

public List<String> get() {
return new ArrayList<>(objects.keySet());
}

@Nullable
public T parse(String key) {
return getObject(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ public record DisplayData(Display.Brightness brightness,
float displayWidth,
float displayHeight,
@Nullable Color glowColorOverride,
boolean applyBrightness) {
boolean applyBrightness,
int interpolationDuration,
int interpolationDelay,
int teleportDuration) {

public static DisplayData DEFAULT = new DisplayData(new Display.Brightness(15, 15),
Display.Billboard.FIXED, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
null, false);
null, false,
1, 0, 1);

/**
* Will apply the DisplayData to the given Display.
Expand All @@ -48,6 +52,12 @@ public void apply(Display display) {
display.setDisplayWidth(displayWidth);
display.setDisplayHeight(displayHeight);
display.setGlowColorOverride(glowColorOverride);
display.setInterpolationDuration(interpolationDuration);
display.setInterpolationDelay(interpolationDelay);
try {
display.setTeleportDuration(teleportDuration);
} catch (Throwable ignored) {
}
}

/**
Expand Down Expand Up @@ -79,8 +89,12 @@ public static DisplayData of(ConfigurationSection section) {
int b = Integer.parseInt(split[2]);
color = Color.fromRGB(r, g, b);
}
int interpolationDuration = section.getInt("Interpolation-Duration", 1);
int interpolationDelay = section.getInt("Interpolation-Delay", 0);
int teleportDuration = section.getInt("Teleport-Duration", 1);
return new DisplayData(brightness, billboard, shadowRadius, shadowStrength,
viewRange, displayWidth, displayHeight, color, applyBrightness);
viewRange, displayWidth, displayHeight, color, applyBrightness,
interpolationDuration, interpolationDelay, teleportDuration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package us.mytheria.bloblib.entities.display;

import org.bukkit.entity.Display;
import org.bukkit.util.Transformation;
import org.jetbrains.annotations.NotNull;
import us.mytheria.bloblib.entities.BukkitPluginOperator;

/**
* Represents an object that can hold Display entities
* using initial displayData and a transformation.
*/
public interface DisplayOperator extends BukkitPluginOperator {

/**
* The initial DisplayData held by this DisplayOperator.
*
* @return The initial DisplayData held by this DisplayOperator.
*/
DisplayData getDisplayData();

/**
* The initial transformation held by this DisplayOperator.
*
* @return The initial transformation held by this DisplayOperator.
*/
Transformation getTransformation();

/**
* Applies the DisplayData and Transformation to the given Display.
*
* @param display the Display to apply the DisplayData and Transformation to
*/
default void apply(@NotNull Display display) {
if (getDisplayData() != null)
getDisplayData().apply(display);
if (getTransformation() != null)
display.setTransformation(getTransformation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package us.mytheria.bloblib.entities.display;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Transformation;

public class DisplayOperatorReader {

public static DisplayOperator READ(ConfigurationSection section, String path, JavaPlugin plugin) {
DisplayData displayData;
try {
displayData = DisplayData.of(section);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e.getMessage() + " in file " + path);
}
Transformation transformation;
try {
transformation = DisplayReader.TRANSFORMATION_FAIL_FAST(section);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage() + " in file " + path);
}
return new DisplayOperator() {
@Override
public DisplayData getDisplayData() {
return displayData;
}

@Override
public Transformation getTransformation() {
return transformation;
}

@Override
public Plugin getPlugin() {
return plugin;
}
};
}
}

0 comments on commit 0d1d0ea

Please sign in to comment.