Skip to content

Commit

Permalink
Added PlotWheel and fixed a bug with visual hopper connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbRandy committed Oct 27, 2024
1 parent 36fac05 commit 4886706
Show file tree
Hide file tree
Showing 68 changed files with 1,216 additions and 691 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
val versions = providers.gradleProperty("net.labymod.minecraft-versions").get().split(";")

group = "tmb.randy"
version = providers.environmentVariable("VERSION").getOrElse("1.6.0")
version = providers.environmentVariable("VERSION").getOrElse("1.7.0")

labyMod {
defaultPackageName = "tmb.randy.tmbgriefergames"
Expand Down
78 changes: 73 additions & 5 deletions core/src/main/java/tmb/randy/tmbgriefergames/core/Addon.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package tmb.randy.tmbgriefergames.core;

import java.util.Objects;
import net.labymod.api.Laby;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.client.gui.hud.binding.category.HudWidgetCategory;
import net.labymod.api.client.gui.screen.activity.types.IngameOverlayActivity;
import net.labymod.api.event.Subscribe;
import net.labymod.api.event.client.chat.ChatReceiveEvent;
import net.labymod.api.event.client.input.KeyEvent;
import net.labymod.api.event.client.input.KeyEvent.State;
import net.labymod.api.event.client.lifecycle.GameTickEvent;
import net.labymod.api.models.addon.annotation.AddonMain;
import tmb.randy.tmbgriefergames.core.activities.plotwheel.PlotWheelActivity;
import tmb.randy.tmbgriefergames.core.activities.plotwheel.PlotWheelPlot;
import tmb.randy.tmbgriefergames.core.commands.AutocraftV2Command;
import tmb.randy.tmbgriefergames.core.commands.AutocraftV3Command;
import tmb.randy.tmbgriefergames.core.commands.DKsCommand;
import tmb.randy.tmbgriefergames.core.commands.EjectCommand;
import tmb.randy.tmbgriefergames.core.commands.PayAllCommand;
import tmb.randy.tmbgriefergames.core.commands.PlayerTracerCommand;
import tmb.randy.tmbgriefergames.core.config.Configuration;
import tmb.randy.tmbgriefergames.core.enums.CBs;
import tmb.randy.tmbgriefergames.core.events.CbChangedEvent;
import tmb.randy.tmbgriefergames.core.generated.DefaultReferenceStorage;
import tmb.randy.tmbgriefergames.core.util.AccountUnity;
import tmb.randy.tmbgriefergames.core.util.HopperTracker;
Expand All @@ -32,15 +42,14 @@
import tmb.randy.tmbgriefergames.core.widgets.HopperModeWidget;
import tmb.randy.tmbgriefergames.core.widgets.ItemClearWidget;
import tmb.randy.tmbgriefergames.core.widgets.NearbyWidget;
import java.util.Objects;

@AddonMain
public class Addon extends LabyAddon<Configuration> {

private IBridge bridge;
private static Addon SharedInstance;
private GameInfoWidget gameInfoWidget;
private final CBtracker CBtracker = new CBtracker();
private final CBtracker cbtracker = new CBtracker();
private final PlayerTracer playerTracer = new PlayerTracer();
private final HopperTracker hopperTracker = new HopperTracker();
private final PlotSwitch plotSwitch = new PlotSwitch();
Expand All @@ -57,15 +66,17 @@ public class Addon extends LabyAddon<Configuration> {
private final AccountUnity accountUnity = new AccountUnity();
private final ItemClearTimerListener itemClearTimerListener = new ItemClearTimerListener();

private final String ADDON_PREFIX = "§6[§5§l§oT§b§l§oM§5§l§oB§6] ";
private static final int commandCountdownLimit = 80;
private static int commandCountdown = 0;
public static PlotWheelPlot queuedPlot = null;

@Override
@Override
protected void enable() {
this.registerSettingCategory();
SharedInstance = this;
bridge = getReferenceStorage().iBridge();
this.registerListener(bridge);
this.registerListener(CBtracker);
this.registerListener(cbtracker);
this.registerListener(playerTracer);
this.registerListener(chatCleaner);
this.registerListener(cooldownNotifier);
Expand All @@ -80,6 +91,7 @@ protected void enable() {
this.registerListener(itemSaver);
this.registerListener(accountUnity);
this.registerListener(itemClearTimerListener);
this.registerListener(this);

this.registerCommand(new DKsCommand());
this.registerCommand(new PayAllCommand());
Expand All @@ -99,6 +111,9 @@ protected void enable() {
labyAPI().hudWidgetRegistry().register(new NearbyWidget(category));
labyAPI().hudWidgetRegistry().register(new HopperModeWidget(category));

//PlotWheelActivity activity = new PlotWheelActivity();
//labyAPI().navigationService().register(new PlotWheelNavigationElement(activity));

this.logger().info("Enabled the Addon");
}

Expand All @@ -108,6 +123,7 @@ protected Class<Configuration> configurationClass() {
}

public void displayNotification(String msg) {
String ADDON_PREFIX = "§6[§5§l§oT§b§l§oM§5§l§oB§6] ";
Laby.labyAPI().minecraft().chatExecutor().displayClientMessage(ADDON_PREFIX + msg);
}

Expand Down Expand Up @@ -146,5 +162,57 @@ public static boolean isChatGuiOpen() {

public PlayerTracer getPlayerTracer() {return playerTracer;}

@Subscribe
public void keyInput(KeyEvent event) {
if(event.state() == State.PRESS && event.key() == configuration().getPlotWheelHotkey().get() && !isChatGuiOpen() && CBtracker.isCommandAbleCB()) {
Laby.labyAPI().minecraft().minecraftWindow().displayScreen(new PlotWheelActivity());
}
}

@Subscribe
public void tick(GameTickEvent event) {
commandCountdown();
}

@Subscribe
public void cbChanged(CbChangedEvent event) {
if(event.CB() == CBs.LOBBY && Addon.getSharedInstance().configuration().getSkipHub().get())
Addon.sendCommand("/portal");
}

@Subscribe
public void messageReceived(ChatReceiveEvent event) {
if(event.chatMessage().getPlainText().equals("[Switcher] Daten heruntergeladen!")) {
if(queuedPlot != null) {
if(CBtracker.isPlotworldCB(CBtracker.getCurrentCB())) {
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
if(isGG() && (queuedPlot.cb() == CBs.NONE || CBtracker.getCurrentCB() == queuedPlot.cb())) {
Laby.references().chatExecutor().chat(queuedPlot.command());
queuedPlot = null;
}
}
}, 500
);
}
}
}
}


public static boolean canSendCommand() { return commandCountdown <= 0; }
public static void sendCommand(String command) {
if(canSendCommand()) {
Laby.references().chatExecutor().chat(command);
commandCountdown = commandCountdownLimit;
}
}

private static void commandCountdown() {
if (commandCountdown > 0) {
commandCountdown--;
}
}
}
38 changes: 23 additions & 15 deletions core/src/main/java/tmb/randy/tmbgriefergames/core/CBtracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ public class CBtracker {
public void worldEnterEvent(ScoreboardTeamUpdateEvent event) {
if(!Addon.isGG()) return;
Scoreboard scoreboard = Laby.labyAPI().minecraft().getScoreboard();
for (ScoreboardTeam team : scoreboard.getTeams()) {
if(team.getTeamName().equals("server_value")) {
String CBString = ((TextComponent)team.getPrefix()).getText();
if(scoreboard != null) {
for (ScoreboardTeam team : scoreboard.getTeams()) {
if(team.getTeamName().equals("server_value")) {
String CBString = ((TextComponent)team.getPrefix()).getText();

CBs newCB = CBs.NONE;
CBs newCB = CBs.NONE;

try {
newCB = CBs.valueOf(CBString.toUpperCase());
} catch (IllegalArgumentException e) {
Addon.getSharedInstance().logger().warn(e.getMessage());
}
try {
newCB = CBs.valueOf(CBString.toUpperCase());
} catch (IllegalArgumentException e) {
Addon.getSharedInstance().logger().warn(e.getMessage());
}

if(newCB != currentCB) {
currentCB = newCB;
Addon.getSharedInstance().logger().info("Joined CityBuild " + newCB.getName());
Laby.fireEvent(new CbChangedEvent(newCB));
if(newCB != currentCB) {
currentCB = newCB;
Addon.getSharedInstance().logger().info("Joined " + newCB.getName());
Laby.fireEvent(new CbChangedEvent(newCB));
}
}
}
}

}

public static CBs getCurrentCB() {return currentCB;}
Expand All @@ -48,10 +49,17 @@ public static boolean isPlotworldCB() {
default -> true;
};
}
public static boolean canSendPayment() {
public static boolean isCommandAbleCB() {
return switch (currentCB) {
case PORTAL, LOBBY, NONE -> false;
default -> true;
};
}

public static boolean isPlotworldCB(CBs cb) {
return switch (cb) {
case WASSER, LAVA, PORTAL, LOBBY, NONE -> false;
default -> true;
};
}
}
101 changes: 86 additions & 15 deletions core/src/main/java/tmb/randy/tmbgriefergames/core/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.labymod.api.Laby;
import tmb.randy.tmbgriefergames.core.activities.plotwheel.PlotWheelPlot;
import tmb.randy.tmbgriefergames.core.enums.CBs;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class FileManager {
private static final File storageFile = new File(new File(Laby.labyAPI().labyModLoader().getGameDirectory().toFile(), "labymod-neo/configs/tmbgriefergames"), "tmbgriefergames.json");
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static Map<String, Object> globalData = new HashMap<>();
private static Map<UUID, Map<String, Object>> playerData = new HashMap<>();

static {
Expand All @@ -24,18 +30,27 @@ public class FileManager {

private static void saveData() {
try {
if (!storageFile.getParentFile().exists()) {
if (!storageFile.getParentFile().exists())
storageFile.getParentFile().mkdirs();
}
if (!storageFile.exists()) {

if (!storageFile.exists())
storageFile.createNewFile();


Map<String, Object> allData = new HashMap<>();
Map<String, Map<String, Object>> playerDataAsStringKeys = new HashMap<>();
for (Map.Entry<UUID, Map<String, Object>> entry : playerData.entrySet()) {
playerDataAsStringKeys.put(entry.getKey().toString(), entry.getValue());
}

allData.put("global", globalData);
allData.put("player", playerDataAsStringKeys);

try (FileWriter writer = new FileWriter(storageFile)) {
gson.toJson(playerData, writer);
gson.toJson(allData, writer);
}
} catch (IOException e) {
e.printStackTrace();
Addon.getSharedInstance().logger().error("FileManager", e);
}
}

Expand All @@ -45,28 +60,84 @@ private static void loadData() {
}

try (FileReader reader = new FileReader(storageFile)) {
Type type = new TypeToken<Map<UUID, Map<String, Object>>>() {}.getType();
playerData = gson.fromJson(reader, type);
if (playerData == null) {
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> allData = gson.fromJson(reader, type);

if (allData != null) {
globalData = (Map<String, Object>) allData.getOrDefault("global", new HashMap<>());

Map<String, Map<String, Object>> rawPlayerData = (Map<String, Map<String, Object>>) allData.getOrDefault("player", new HashMap<>());
playerData = new HashMap<>();
for (Map.Entry<String, Map<String, Object>> entry : rawPlayerData.entrySet()) {
UUID playerUUID = UUID.fromString(entry.getKey());
playerData.put(playerUUID, entry.getValue());
}
} else {
globalData = new HashMap<>();
playerData = new HashMap<>();
}
} catch (IOException e) {
e.printStackTrace();
Addon.getSharedInstance().logger().error("FileManager", e);
} catch (ClassCastException e) {
Addon.getSharedInstance().logger().error("FileManager - Invalid format", e);
}
}

public static void setGlobalValue(String key, Object value) {
globalData.put(key, value);
saveData();
}

public static Object getGlobalValue(String key) {
return globalData.get(key);
}

public static void addPlot(PlotWheelPlot plot) {
List<PlotWheelPlot> plots = loadPlots();
plots.add(plot);
setGlobalValue("PlotWheel", plots);
}

public static ArrayList<PlotWheelPlot> loadPlots() {
Object value = getGlobalValue("PlotWheel");
ArrayList<PlotWheelPlot> plots = new ArrayList<>();

if (value instanceof List<?>) {
Gson gson = new Gson();
Type listType = new TypeToken<List<Map<String, Object>>>() {}.getType();
List<Map<String, Object>> plotDataList = gson.fromJson(gson.toJson(value), listType);

for (Map<String, Object> plotData : plotDataList) {
CBs cb = CBs.valueOf((String) plotData.get("cb"));
String name = (String) plotData.get("name");
String command = (String) plotData.get("command");
UUID account = plotData.get("account") != null ? UUID.fromString((String) plotData.get("account")) : null;

plots.add(new PlotWheelPlot(cb, name, command, account));
}
}

return plots;
}

public static void deletePlot(PlotWheelPlot plot) {
List<PlotWheelPlot> plots = loadPlots();
plots.removeIf(plott -> plott.toString().equals(plot.toString()));
setGlobalValue("PlotWheel", plots);
}

public static void setValue(String key, Object value) {
if(Laby.labyAPI().minecraft().getClientPlayer() != null) {
UUID playerUUID = Laby.labyAPI().minecraft().getClientPlayer().profile().getUniqueId();
public static void setPlayerValue(String key, Object value) {
if (Laby.labyAPI().minecraft().getClientPlayer() != null) {
UUID playerUUID = Objects.requireNonNull(Laby.labyAPI().minecraft().getClientPlayer()).profile().getUniqueId();
Map<String, Object> values = playerData.computeIfAbsent(playerUUID, k -> new HashMap<>());
values.put(key, value);
saveData();
}
}

public static Object getValue(String key) {
if(Laby.labyAPI().minecraft().getClientPlayer() != null) {
UUID playerUUID = Laby.labyAPI().minecraft().getClientPlayer().profile().getUniqueId();
public static Object getPlayerValue(String key) {
if (Laby.labyAPI().minecraft().getClientPlayer() != null) {
UUID playerUUID = Objects.requireNonNull(Laby.labyAPI().minecraft().getClientPlayer()).profile().getUniqueId();
Map<String, Object> values = playerData.get(playerUUID);
return values != null ? values.get(key) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IBridge {
void startAuswurf();
void startAutocrafterV3();
void openChat();
boolean isChatGuiOpen();
boolean isChatGuiClosed();
void resetLines();
boolean allKeysPressed(Key[] keys);
}
Loading

0 comments on commit 4886706

Please sign in to comment.