Skip to content

Commit

Permalink
feat: can create basic portals with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Dec 18, 2023
1 parent 21e7481 commit ad7a482
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.sekwah.advancedportals.core.module.AdvancedPortalsModule;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.services.DestinationServices;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.tags.activation.DestiTag;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.util.GameScheduler;
Expand Down Expand Up @@ -51,6 +52,9 @@ public class AdvancedPortalsCore {
@Inject
private TagRegistry tagRegistry;

@Inject
private PortalServices portalServices;

@Inject
private DestinationServices destinationServices;

Expand Down Expand Up @@ -97,6 +101,7 @@ public void onEnable() {
this.registerCommands();
this.registerTags();

this.portalServices.loadPortals();
this.destinationServices.loadDestinations();
this.infoLogger.log(Lang.translate("logger.pluginenable"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
ArrayList<DataTag> destinationTags = TagReader.getTagsFromArgs(args);

// Find the tag with the "name" NAME
DataTag nameTag = destinationTags.stream().findFirst().filter(tag -> tag.NAME.equals("name")).orElse(null);
DataTag nameTag = destinationTags.stream().filter(tag -> tag.NAME.equals("name")).findFirst().orElse(null);

// If the tag is null, check if arg[1] has a : to check it's not a tag.
if(nameTag == null && !args[1].contains(":")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class RemoveDestiSubCommand implements SubCommand {
public void onCommand(CommandSenderContainer sender, String[] args) {
if(args.length > 1) {
if(destinationServices.removeDestination(args[1], sender.getPlayerContainer())) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.portal.remove.complete"));
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.destination.remove.complete"));
}
else {
sender.sendMessage(Lang.translate("messageprefix.negative")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void registered() {
for (Destination destination : destinationServices.getDestinations()) {
var pos = destination.getLoc();
if(pos.distanceTo(player.getLoc()) < config.getVisibleRange()) {
Debug.addMarker(player, pos.toBlockPos(), destination.getArgValues("name")[0], new Color(100, 100, 100, 100), 1000);
Debug.addMarker(player, pos.toBlockPos(), destination.getArgValues("name")[0], new Color(100, 100, 100, 100), 1300);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.sekwah.advancedportals.core.permissions.PortalPermissions;
import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.util.InfoLogger;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.util.TagReader;
import com.sekwah.advancedportals.core.warphandler.Tag;
Expand All @@ -25,6 +27,9 @@ public class CreatePortalSubCommand extends CreateTaggedSubCommand {
@Inject
TagRegistry tagRegistry;

@Inject
InfoLogger infoLogger;

@Override
public void onCommand(CommandSenderContainer sender, String[] args) {
if(args.length > 1) {
Expand All @@ -36,7 +41,11 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
ArrayList<DataTag> portalTags = TagReader.getTagsFromArgs(args);

// Find the tag with the "name" NAME
DataTag nameTag = portalTags.stream().findFirst().filter(tag -> tag.NAME.equals("name")).orElse(null);
DataTag nameTag = portalTags.stream().filter(tag -> {
this.infoLogger.log("Tag: " + tag.NAME);
this.infoLogger.log("Equals: " + tag.NAME.equals(NameTag.TAG_NAME));
return tag.NAME.equals(NameTag.TAG_NAME);
}).findFirst().orElse(null);

// If the tag is null, check if arg[1] has a : to check it's not a tag.
if(nameTag == null && !args[1].contains(":")) {
Expand All @@ -63,10 +72,11 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
if(portal != null) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.create.complete"));
sender.sendMessage(Lang.translate("command.create.tags"));
sender.sendMessage("\u00A7a" + "triggerBlock\u00A77:\u00A7e" + Arrays.toString(portal.getTriggerBlocks()));
sender.sendMessage("\u00A7a" + " triggerBlock\u00A77:\u00A7e" + Arrays.toString(portal.getTriggerBlocks()));
this.printTags(sender, portal.getArgs());
} else {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.create.error"));
}
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.create.error"));
}
else {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.error.notags"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ListPortalsSubCommand implements SubCommand {
@Override
public void onCommand(CommandSenderContainer sender, String[] args) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.portal.list")
+ " " + portalServices.getPortals().asList().stream().map(Map.Entry::getKey).sorted().collect(Collectors.joining(", ")));
+ " " + portalServices.getPortalNames().stream().sorted().collect(Collectors.joining(", ")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
}
}
else {
PlayerContainer player = sender.getPlayerContainer();
if(player == null) {
sender.sendMessage(Lang.translate("command.portal.remove.noname"));
}
else {
if(portalServices.removePlayerSelection(player)) {

}
else {
sender.sendMessage(Lang.translate("messageprefix.negative")
+ Lang.translate("command.portal.remove.error"));
}
}
sender.sendMessage(Lang.translate("command.portal.remove.noname"));
}
}

Expand All @@ -55,12 +43,7 @@ public boolean hasPermission(CommandSenderContainer sender) {

@Override
public List<String> onTabComplete(CommandSenderContainer sender, String[] args) {
List<String> portalNames = new ArrayList<>();
for(Map.Entry<String, AdvancedPortal> portal : portalServices.getPortals()) {
portalNames.add(portal.getKey());
}
Collections.sort(portalNames);
return portalNames;
return portalServices.getPortalNames();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.connector.containers.ServerContainer;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.PlayerTempData;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.services.PortalTempDataServices;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.util.Debug;
import com.sekwah.advancedportals.core.util.GameScheduler;
import com.sekwah.advancedportals.core.util.Lang;
Expand All @@ -21,6 +24,8 @@
*/
public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOnInit {

static final int SHOW_TICKS = 1300;

@Inject
PortalTempDataServices tempDataServices;

Expand All @@ -33,6 +38,12 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn
@Inject
ServerContainer serverContainer;

@Inject
PortalServices portalServices;

@Inject
ConfigRepository config;

@Override
public void onCommand(CommandSenderContainer sender, String[] args) {
if(core.getMcVersion()[1] < 16) {
Expand Down Expand Up @@ -77,40 +88,53 @@ public void registered() {
if(!tempData.isPortalVisible()) {
continue;
}
for (var portal : portalServices.getPortals()) {
if(portal.isLocationInPortal(player.getLoc(), config.getVisibleRange())) {
BlockLocation minLoc = portal.getMinLoc();
BlockLocation maxLoc = portal.getMaxLoc();
int midX = (minLoc.posX + maxLoc.posX) / 2;
int midZ = (minLoc.posZ + maxLoc.posZ) / 2;
BlockLocation midPoint = new BlockLocation(minLoc.worldName, midX, maxLoc.posY, midZ);
var color = new Color(0, 255, 0, 100);
debugPortal(player, portal.getMinLoc(), portal.getMaxLoc(), color, 1000, false);
Debug.addMarker(player, midPoint, portal.getArgValues(NameTag.TAG_NAME)[0], color, SHOW_TICKS);
}
}

if(tempData.getPos1() != null) {
Debug.addMarker(player, tempData.getPos1(), "Pos1", new Color(0, 255, 0), 1000);
Debug.addMarker(player, tempData.getPos1(), "Pos1", new Color(0, 255, 0), SHOW_TICKS);
}
if(tempData.getPos2() != null) {
Debug.addMarker(player, tempData.getPos2(), "Pos2", new Color(255, 0, 0), 1000);
Debug.addMarker(player, tempData.getPos2(), "Pos2", new Color(255, 0, 0), SHOW_TICKS);
}

if (tempData.getPos1() != null && tempData.getPos2() != null) {
int minX = Math.min(tempData.getPos1().posX, tempData.getPos2().posX);
int minY = Math.min(tempData.getPos1().posY, tempData.getPos2().posY);
int minZ = Math.min(tempData.getPos1().posZ, tempData.getPos2().posZ);

int maxX = Math.max(tempData.getPos1().posX, tempData.getPos2().posX);
int maxY = Math.max(tempData.getPos1().posY, tempData.getPos2().posY);
int maxZ = Math.max(tempData.getPos1().posZ, tempData.getPos2().posZ);

for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
if ((x == minX || x == maxX) && (y == minY || y == maxY || z == minZ || z == maxZ) ||
(y == minY || y == maxY) && (x == minX || x == maxX || z == minZ || z == maxZ) ||
(z == minZ || z == maxZ) && (x == minX || x == maxX || y == minY || y == maxY)) {

var pos = new BlockLocation(tempData.getPos1().worldName, x, y, z);
if (pos.equals(tempData.getPos1()) || pos.equals(tempData.getPos2()))
continue;
Debug.addMarker(player, pos, "", new Color(255, 0, 0, 100), 1000);
}
}
}
}
debugPortal(player, tempData.getPos1(), tempData.getPos2(), new Color(255, 0, 0, 100), SHOW_TICKS, true);
}
}
}, 1, 20);
}

private static void debugPortal(PlayerContainer player, BlockLocation pos1, BlockLocation pos2, Color color, int time, boolean hideCorners) {
int minX = Math.min(pos1.posX, pos2.posX);
int minY = Math.min(pos1.posY, pos2.posY);
int minZ = Math.min(pos1.posZ, pos2.posZ);

int maxX = Math.max(pos1.posX, pos2.posX);
int maxY = Math.max(pos1.posY, pos2.posY);
int maxZ = Math.max(pos1.posZ, pos2.posZ);

for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
if ((y == minY || y == maxY) && (x == minX || x == maxX || z == minZ || z == maxZ) || (z == minZ || z == maxZ) && (x == minX || x == maxX)) {
var pos = new BlockLocation(pos1.worldName, x, y, z);
if ((pos.equals(pos1) || pos.equals(pos2)) && hideCorners)
continue;
Debug.addMarker(player, pos, "", color, time);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.registry.TagRegistry;
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;

Expand Down Expand Up @@ -34,9 +35,8 @@ public class AdvancedPortal implements TagTarget {
@SerializedName("a")
private HashMap<String, String[]> args = new HashMap<>();

public AdvancedPortal(BlockLocation maxLoc, BlockLocation minLoc) {
this.maxLoc = maxLoc;
this.minLoc = minLoc;
public AdvancedPortal(BlockLocation minLoc, BlockLocation maxLoc) {
this.updateBounds(minLoc, maxLoc);
}

public BlockLocation getMaxLoc() {
Expand Down Expand Up @@ -67,6 +67,25 @@ public void removeArg(String arg) {
this.args.remove(arg);
}

/**
* Updates the bounds of the portal based on the provided locations.
*
* @param loc1 The first location.
* @param loc2 The second location.
*/
public void updateBounds(BlockLocation loc1, BlockLocation loc2) {
int minX = Math.min(loc1.posX, loc2.posX);
int minY = Math.min(loc1.posY, loc2.posY);
int minZ = Math.min(loc1.posZ, loc2.posZ);

int maxX = Math.max(loc1.posX, loc2.posX);
int maxY = Math.max(loc1.posY, loc2.posY);
int maxZ = Math.max(loc1.posZ, loc2.posZ);

this.minLoc = new BlockLocation(loc1.worldName, minX, minY, minZ);
this.maxLoc = new BlockLocation(loc2.worldName, maxX, maxY, maxZ);
}

public boolean hasTriggerBlock(String blockMaterial) {
for(String triggerBlock : triggerBlocks) {
if(blockMaterial.equals(triggerBlock)) {
Expand Down Expand Up @@ -105,6 +124,24 @@ public boolean activate(PlayerContainer player) {
return true;
}

public boolean isLocationInPortal(PlayerLocation playerLocation) {
return this.isLocationInPortal(playerLocation, 0);
}

public boolean isLocationInPortal(PlayerLocation playerLocation, int additionalArea) {
double playerX = playerLocation.getPosX();
double playerY = playerLocation.getPosY();
double playerZ = playerLocation.getPosZ();

return playerX >= this.minLoc.posX - additionalArea &&
playerX < this.maxLoc.posX + 1 + additionalArea &&
playerY >= this.minLoc.posY - additionalArea &&
playerY < this.maxLoc.posY + 1 + additionalArea &&
playerZ >= this.minLoc.posZ - additionalArea &&
playerZ < this.maxLoc.posZ + 1 + additionalArea;
}


public void setArgValues(DataTag portalTag) {
this.setArgValues(portalTag.NAME, portalTag.VALUES);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sekwah.advancedportals.core.repository;

import com.sekwah.advancedportals.core.serializeddata.WorldLocation;
import com.sekwah.advancedportals.core.portal.AdvancedPortal;

public interface IPortalRepository extends IJsonRepository<WorldLocation> {
public interface IPortalRepository extends IJsonRepository<AdvancedPortal> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public class DestinationRepositoryImpl implements IDestinationRepository {
@Inject
DataStorage dataStorage;

@Inject
InfoLogger infoLogger;

@Override
public boolean save(String name, Destination destination) {
return dataStorage.storeJson(destination, fileLocation + name + ".json");
Expand All @@ -44,8 +41,8 @@ public boolean update(String name, Destination destination) {
return false;
}

public Destination get(String desti) {
return dataStorage.loadJson(Destination.class, fileLocation + desti + ".json");
public Destination get(String name) {
return dataStorage.loadJson(Destination.class, fileLocation + name + ".json");
}

@Override
Expand All @@ -56,13 +53,13 @@ public List<String> getAllNames() {
@Override
public List<Destination> getAll() {
List<Destination> destinations = new ArrayList<>();
List<String> allFiles = dataStorage.listAllFiles(fileLocation, false);
List<String> allFiles = dataStorage.listAllFiles(fileLocation, true);
for (String fileName : allFiles) {
Destination destination = dataStorage.loadJson(Destination.class, fileLocation + fileName);
Destination destination = this.get(fileName);
// Forces the name tag to be up-to-date on load
String[] name = destination.getArgValues(NameTag.TAG_NAME);
if(name != null && name.length > 0) {
destination.setArgValues(NameTag.TAG_NAME, new String[]{fileName.replace(".json", "")});
destination.setArgValues(NameTag.TAG_NAME, new String[]{fileName});
}
destinations.add(destination);
}
Expand Down
Loading

0 comments on commit ad7a482

Please sign in to comment.