Skip to content

Commit

Permalink
So much refactoring
Browse files Browse the repository at this point in the history
It'll be worth it... eventually
  • Loading branch information
MoSadie committed Aug 4, 2024
1 parent f6fc5e7 commit ff9db5d
Show file tree
Hide file tree
Showing 66 changed files with 2,277 additions and 1,281 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mosadie.effectmc.core;

import com.google.gson.JsonObject;
import com.mosadie.effectmc.core.handler.*;
import com.mosadie.effectmc.core.effect.*;
import com.mosadie.effectmc.core.handler.Device;
import com.mosadie.effectmc.core.handler.DeviceType;

import java.net.URL;

Expand All @@ -10,8 +12,8 @@ public interface EffectExecutor {

boolean joinServer(String serverIp);

boolean setSkinLayer(SkinLayerHandler.SKIN_SECTION section, boolean visibility);
boolean toggleSkinLayer(SkinLayerHandler.SKIN_SECTION section);
boolean setSkinLayer(SkinLayerEffect.SKIN_SECTION section, boolean visibility);
boolean toggleSkinLayer(SkinLayerEffect.SKIN_SECTION section);

boolean sendChatMessage(String message);
boolean receiveChatMessage(String message);
Expand All @@ -20,9 +22,9 @@ public interface EffectExecutor {

boolean showActionMessage(String message);

void showTrustPrompt(String device, DeviceType type);
void showTrustPrompt(Device device);

boolean triggerDisconnect(DisconnectHandler.NEXT_SCREEN nextScreen, String title, String message);
boolean triggerDisconnect(DisconnectEffect.NEXT_SCREEN nextScreen, String title, String message);

boolean playSound(String soundID, String categoryName, float volume, float pitch, boolean repeat, int repeatDelay, String attenuationType, double x, double y, double z, boolean relative, boolean global);

Expand All @@ -42,19 +44,19 @@ public interface EffectExecutor {

//boolean refreshSkin(UUID uuid);

boolean setSkin(URL url, SetSkinHandler.SKIN_TYPE skinType);
boolean setSkin(URL url, SetSkinEffect.SKIN_TYPE skinType);

boolean openScreen(OpenScreenHandler.SCREEN screen);
boolean openScreen(OpenScreenEffect.SCREEN screen);

boolean setFOV(int fov);

boolean setPOV(SetPovHandler.POV pov);
boolean setPOV(SetPovEffect.POV pov);

boolean setGuiScale(int scale);

boolean setGamma(double gamma);

boolean setChatVisibility(ChatVisibilityHandler.VISIBILITY visibility);
boolean setChatVisibility(ChatVisibilityEffect.VISIBILITY visibility);

boolean setRenderDistance(int chunks);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.mosadie.effectmc.core.handler.*;
import com.mosadie.effectmc.core.effect.*;
import com.mosadie.effectmc.core.effect.internal.Effect;
import com.mosadie.effectmc.core.effect.internal.EffectRequest;
import com.mosadie.effectmc.core.handler.Device;
import com.mosadie.effectmc.core.handler.DeviceType;
import com.mosadie.effectmc.core.handler.TrustHandler;
import com.mosadie.effectmc.core.handler.http.*;
import com.sun.net.httpserver.HttpServer;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;

Expand All @@ -22,18 +28,22 @@ public class EffectMCCore {
private final EffectExecutor executor;
private final Gson gson;

private final List<EffectRequestHandler> effects;
public Gson getGson() {
return gson;
}

private final List<Effect> effects;

private HttpServer server;

private Map<DeviceType, Set<String>> trustedDevices;
private TrustHandler trustHandler;
private boolean trustNextRequest;

public EffectMCCore(File configFile, File trustFile, EffectExecutor executor) {
this.configFile = configFile;
this.trustFile = trustFile;
this.executor = executor;
trustedDevices = new HashMap<>();
trustedDevices = new HashMap<>(); // This is where I left off

for (DeviceType type : DeviceType.values()) {
trustedDevices.put(type, new HashSet<>());
Expand All @@ -44,30 +54,30 @@ public EffectMCCore(File configFile, File trustFile, EffectExecutor executor) {
gson = new Gson();

effects = new ArrayList<>();
effects.add(new JoinServerHandler(this));
effects.add(new SkinLayerHandler(this));
effects.add(new SendChatMessageHandler(this));
effects.add(new ReceiveChatMessageHandler(this));
effects.add(new ShowTitleHandler(this));
effects.add(new ShowActionMessageHandler(this));
effects.add(new DisconnectHandler(this));
effects.add(new PlaySoundHandler(this));
effects.add(new StopSoundHandler(this));
effects.add(new ShowToastHandler(this));
effects.add(new OpenBookHandler(this));
effects.add(new NarrateHandler(this));
effects.add(new LoadWorldHandler(this));
effects.add(new SetSkinHandler(this));
effects.add(new OpenScreenHandler(this));
effects.add(new SetFovHandler(this));
effects.add(new SetPovHandler(this));
effects.add(new SetGUIScaleHandler(this));
effects.add(new SetGammaHandler(this));
effects.add(new SetGameModeHandler(this));
effects.add(new ChatVisibilityHandler(this));
effects.add(new SetRenderDistanceHandler(this));
effects.add(new RejoinHandler(this));
effects.add(new ShowItemToastHandler(this));
effects.add(new JoinServerEffect());
effects.add(new SkinLayerEffect());
effects.add(new SendChatMessageEffect());
effects.add(new ReceiveChatMessageEffect());
effects.add(new ShowTitleEffect());
effects.add(new ShowActionMessageEffect());
effects.add(new DisconnectEffect());
effects.add(new PlaySoundEffect());
effects.add(new StopSoundEffect());
effects.add(new ShowToastEffect());
effects.add(new OpenBookEffect());
effects.add(new NarrateEffect());
effects.add(new LoadWorldEffect());
effects.add(new SetSkinEffect());
effects.add(new OpenScreenEffect());
effects.add(new SetFovEffect());
effects.add(new SetPovEffect());
effects.add(new SetGUIScaleEffect());
effects.add(new SetGammaEffect());
effects.add(new SetGameModeEffect());
effects.add(new ChatVisibilityEffect());
effects.add(new SetRenderDistanceEffect());
effects.add(new RejoinEffect());
effects.add(new ShowItemToastEffect());
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -142,8 +152,8 @@ public boolean initServer() throws URISyntaxException {

server.createContext("/style.css", new CSSRequestHandler());

for(EffectRequestHandler effect : effects) {
server.createContext("/" + effect.getEffectSlug(), effect);
for(Effect effect : effects) {
server.createContext("/" + effect.getEffectId(), new EffectRequestHandler(this, effect));
}

server.setExecutor(null);
Expand Down Expand Up @@ -203,7 +213,7 @@ public void addTrustedDevice(String device, DeviceType type) {
}
}

public List<EffectRequestHandler> getEffects() {
public List<Effect> getEffects() {
return effects;
}

Expand All @@ -229,15 +239,13 @@ public void accept(boolean t) {
}
}

public boolean executeFromChatMessage(String effectSlug, String worldId, List<String> args) {
if (!checkTrust(worldId, DeviceType.fromWorldState(executor.getWorldState()))) {
return false;
}
for (EffectRequestHandler effect : effects) {
if (effect.getEffectSlug().equals(effectSlug)) {
return effect.executeFromArgs(worldId, args);
public Effect.EffectResult triggerEffect(Device device, EffectRequest request) {
for (Effect effect : effects) {
if (effect.getEffectId().equals(request.getEffectId())) {
return effect.execute(this, request.getArgs(), device);
}
}
return false;

return new Effect.EffectResult("Effect not found.", Effect.EffectResult.Result.ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mosadie.effectmc.core;

import com.mosadie.effectmc.core.handler.DeviceType;
import com.sun.net.httpserver.HttpExchange;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.mosadie.effectmc.core.handler;
package com.mosadie.effectmc.core.effect;

import com.mosadie.effectmc.core.EffectMCCore;
import com.mosadie.effectmc.core.effect.internal.Effect;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChatVisibilityHandler extends EffectRequestHandler {
public class ChatVisibilityEffect extends Effect {

public ChatVisibilityHandler(EffectMCCore core) {
super(core);
addSelectionProperty("visibility", VISIBILITY.SHOW.name(), true, "Visibility", VISIBILITY.toStringArray());
public ChatVisibilityEffect() {
super();
getPropertyManager().addSelectionProperty("visibility", VISIBILITY.SHOW.name(), true, "Visibility", VISIBILITY.toStringArray());
getPropertyManager().lock();
}

@Override
Expand All @@ -18,7 +21,7 @@ public String getEffectName() {
}

@Override
public String getEffectSlug() {
public String getEffectId() {
return "showchat";
}

Expand All @@ -28,12 +31,15 @@ public String getEffectTooltip() {
}

@Override
EffectResult execute() {
core.getExecutor().log("Setting chat visibility: " + getProperty("visibility").getAsString());
if (core.getExecutor().setChatVisibility(VISIBILITY.fromString(getProperty("visibility").getAsString())))
return new EffectResult("Set chat visibility: " + getProperty("visibility").getAsString(), true);
public EffectResult execute(EffectMCCore core, Map<String, Object> args) {
if (!getPropertyManager().argumentCheck(args)) {
return new EffectResult("Invalid Arguments", EffectResult.Result.ERROR);
}
core.getExecutor().log("Setting chat visibility: " + getPropAsString(args, "visibility"));
if (core.getExecutor().setChatVisibility(VISIBILITY.fromString(getPropAsString(args, "visibility"))))
return new EffectResult("Set chat visibility: " + getPropAsString(args, "visibility"), EffectResult.Result.SUCCESS);
else
return new EffectResult("Failed to set chat visibility", false);
return new EffectResult("Failed to set chat visibility", EffectResult.Result.ERROR);
}

public enum VISIBILITY {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.mosadie.effectmc.core.effect;

import com.mosadie.effectmc.core.EffectMCCore;
import com.mosadie.effectmc.core.effect.internal.Effect;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class DisconnectEffect extends Effect {


public DisconnectEffect() {
super();
getPropertyManager().addCommentProperty("Set color using &sect; color codes.");
getPropertyManager().addStringProperty("title", "", true, "Title", "Title");
getPropertyManager().addStringProperty("message", "", true, "Message", "Message");
getPropertyManager().addCommentProperty("For a blank line, use a single space.");
getPropertyManager().addSelectionProperty("nextscreen", NEXT_SCREEN.MAIN_MENU.name(), true, "Next Screen", NEXT_SCREEN.toStringArray());
getPropertyManager().lock();
}

@Override
public String getEffectName() {
return "Disconnect";
}

@Override
public String getEffectId() {
return "triggerdisconnect";
}

@Override
public String getEffectTooltip() {
return "Disconnect from server/world and show a custom disconnect screen.";
}

@Override
public EffectResult execute(EffectMCCore core, Map<String, Object> args) {
if (!getPropertyManager().argumentCheck(args)) {
return new EffectResult("Invalid Arguments", EffectResult.Result.ERROR);
}

NEXT_SCREEN nextScreen = NEXT_SCREEN.getFromName(getPropAsString(args, "nextscreen"));

if (nextScreen == null) {
core.getExecutor().log("Next Screen invalid");
return new EffectResult("Next Screen Invalid", EffectResult.Result.ERROR);
}

core.getExecutor().log("Triggering Disconnect");
if (core.getExecutor().triggerDisconnect(nextScreen, getPropAsString(args, "title"), getPropAsString(args, "message"))) {
return new EffectResult("Disconnected from world.", EffectResult.Result.SUCCESS);
} else {
return new EffectResult("Failed to disconnect.", EffectResult.Result.ERROR);
}
}

public enum NEXT_SCREEN {
MAIN_MENU,
SERVER_SELECT,
WORLD_SELECT;

public static DisconnectEffect.NEXT_SCREEN getFromName(String name) {
try {
return DisconnectEffect.NEXT_SCREEN.valueOf(name.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}

public static String[] toStringArray() {
List<String> list = new ArrayList<>();
for (NEXT_SCREEN screen : NEXT_SCREEN.values()) {
list.add(screen.name());
}
return list.toArray(new String[0]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mosadie.effectmc.core.effect;

import com.mosadie.effectmc.core.EffectMCCore;
import com.mosadie.effectmc.core.effect.internal.Effect;

import java.util.Map;

public class JoinServerEffect extends Effect {

public JoinServerEffect() {
super();
getPropertyManager().addStringProperty("serverip", "", true, "Server IP", "localhost:25565");
getPropertyManager().lock();
}

@Override
public String getEffectName() {
return "Join Server";
}

@Override
public String getEffectTooltip() {
return "Automatically attempts to join the provided Minecraft server.";
}

@Override
public EffectResult execute(EffectMCCore core, Map<String, Object> args) {
if (!getPropertyManager().argumentCheck(args)) {
return new EffectResult("Invalid Arguments", EffectResult.Result.ERROR);
}

if (args.containsKey("serverip")) {
core.getExecutor().log("Joining Server");
if (core.getExecutor().joinServer(getPropAsString(args, "serverip")))
return new EffectResult("Joining Server", EffectResult.Result.SUCCESS);
else
return new EffectResult("Failed to join server.", EffectResult.Result.ERROR);
}

return new EffectResult("Something went wrong.", EffectResult.Result.ERROR);
}
}
Loading

0 comments on commit ff9db5d

Please sign in to comment.