Skip to content

Commit

Permalink
Add region API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Jun 12, 2022
1 parent 57fbcc8 commit f08302f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.craftmend.openaudiomc.api.exceptions;

public class RegionException extends Exception {

public RegionException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.craftmend.openaudiomc.api.impl;

import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.api.exceptions.RegionException;
import com.craftmend.openaudiomc.api.interfaces.IAccountProvider;
import com.craftmend.openaudiomc.api.interfaces.RegistryApi;
import com.craftmend.openaudiomc.generic.authentication.AuthenticationService;
import com.craftmend.openaudiomc.generic.commands.CommandService;
import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand;
import com.craftmend.openaudiomc.generic.database.DatabaseService;
import com.craftmend.openaudiomc.generic.media.MediaService;
import com.craftmend.openaudiomc.generic.media.interfaces.UrlMutation;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService;
import com.craftmend.openaudiomc.generic.platform.Platform;
import com.craftmend.openaudiomc.generic.utils.data.Filter;
import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot;
import com.craftmend.openaudiomc.spigot.modules.regions.objects.RegionProperties;
import com.craftmend.openaudiomc.spigot.modules.regions.objects.TimedRegionProperties;
import com.craftmend.openaudiomc.spigot.modules.shortner.AliasService;
import com.craftmend.openaudiomc.spigot.modules.shortner.data.Alias;
import com.craftmend.openaudiomc.spigot.modules.voicechat.SpigotVoiceChatService;
import lombok.Getter;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class RegistryApiImpl implements RegistryApi {
Expand Down Expand Up @@ -55,4 +62,65 @@ public void forceNetworkingInterface(Class<? extends NetworkingService> service)
public void registerTokenProvider(IAccountProvider provider) {
AuthenticationService.TOKEN_PROVIDER = provider;
}

@Override
public void removeRegion(String regionName) throws RegionException {
if (OpenAudioMc.getInstance().getPlatform() != Platform.SPIGOT) throw new RegionException("This API functionality is only accessible on Spigot");
OpenAudioMcSpigot openAudioMcSpigot = OpenAudioMcSpigot.getInstance();
if (!openAudioMcSpigot.getRegionModule().getRegionAdapter().doesRegionExist(regionName)) {
throw new RegionException("The region " + regionName + " isn't registered in your region provider plugin");
}

regionName = regionName.toLowerCase();
RegionProperties rp = openAudioMcSpigot.getRegionModule().getRegionPropertiesMap().get(regionName);
if (rp != null) {
OpenAudioMc.getService(DatabaseService.class).getRepository(RegionProperties.class)
.delete(rp);
if (rp instanceof TimedRegionProperties) {
((TimedRegionProperties) rp).destroy();
}
openAudioMcSpigot.getRegionModule().removeRegion(regionName);
openAudioMcSpigot.getRegionModule().forceUpdateRegions();
}
}

@Override
public void registerTempRegion(TimedRegionProperties regionProperties) throws RegionException {
if (OpenAudioMc.getInstance().getPlatform() != Platform.SPIGOT) throw new RegionException("This API functionality is only accessible on Spigot");
OpenAudioMcSpigot openAudioMcSpigot = OpenAudioMcSpigot.getInstance();
String regionName = regionProperties.getRegionName().toLowerCase();
// check if this region already is defined
RegionProperties existingRegionProperties = OpenAudioMcSpigot.getInstance().getRegionModule().getRegionPropertiesMap().get(regionName);
if (existingRegionProperties != null) {
openAudioMcSpigot.getRegionModule().removeRegion(regionName);
if (existingRegionProperties instanceof TimedRegionProperties) {
// reset it, because fuck it
TimedRegionProperties timedRegion = (TimedRegionProperties) existingRegionProperties;
openAudioMcSpigot.getRegionModule().removeRegion(regionName);
timedRegion.destroy();
}
}

if (!openAudioMcSpigot.getRegionModule().getRegionAdapter().doesRegionExist(regionName)) {
throw new RegionException("The region " + regionName + " isn't registered in your region provider plugin");
}

openAudioMcSpigot.getRegionModule().registerRegion(regionName, regionProperties);
openAudioMcSpigot.getRegionModule().forceUpdateRegions();
}

@Override
public void registerRegion(RegionProperties regionProperties) throws RegionException {
if (OpenAudioMc.getInstance().getPlatform() != Platform.SPIGOT) throw new RegionException("This API functionality is only accessible on Spigot");
OpenAudioMcSpigot openAudioMcSpigot = OpenAudioMcSpigot.getInstance();
String regionName = regionProperties.getRegionName().toLowerCase();
if (!openAudioMcSpigot.getRegionModule().getRegionAdapter().doesRegionExist(regionName)) {
throw new RegionException("The region " + regionName + " isn't registered in your region provider plugin");
}
OpenAudioMc.getService(DatabaseService.class).getRepository(RegionProperties.class)
.save(regionProperties);
openAudioMcSpigot.getRegionModule().registerRegion(regionName, regionProperties);
openAudioMcSpigot.getRegionModule().forceUpdateRegions();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.craftmend.openaudiomc.api.interfaces;

import com.craftmend.openaudiomc.api.exceptions.RegionException;
import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand;
import com.craftmend.openaudiomc.generic.media.interfaces.UrlMutation;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService;
import com.craftmend.openaudiomc.generic.utils.data.Filter;
import com.craftmend.openaudiomc.spigot.modules.regions.objects.RegionProperties;
import com.craftmend.openaudiomc.spigot.modules.regions.objects.TimedRegionProperties;
import org.bukkit.entity.Player;

public interface RegistryApi {
Expand Down Expand Up @@ -60,4 +63,28 @@ public interface RegistryApi {
*/
void registerTokenProvider(IAccountProvider provider);

/**
* Unregister region media
*
* @param regionName Name of the region
* @throws RegionException Thrown if the region doesn't exist (in worldguard) or if there is no registered region provider
*/
void removeRegion(String regionName) throws RegionException;

/**
* Register a temp region, like it's done with /oa region temp
*
* @param regionProperties Region properties with timing metadata
* @throws RegionException Thrown if the region doesn't exist (in worldguard) or if there is no registered region provider
*/
void registerTempRegion(TimedRegionProperties regionProperties) throws RegionException;

/**
* Register a region, like it's done with /oa region create
*
* @param regionProperties Region metadata
* @throws RegionException Thrown if the region doesn't exist (in worldguard) or if there is no registered region provider
*/
void registerRegion(RegionProperties regionProperties) throws RegionException;

}

0 comments on commit f08302f

Please sign in to comment.