Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Towny contexts #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,56 @@ placeholderapi-placeholders:

> allowflight=true

___
___
#### TownyAdvanced
#### `towny:in-town`
Returns true if player's location within a Town

e.g.

> towny:in-town=false

#### `towny:town`
Returns name of the town (if any) at a player's current location

e.g.

> towny:town=
>
> towny:town=Atlantis

**config:**
```yaml
towny-town: true
```

#### `towny:in-nation`
Returns true if player's location within a Nation

e.g.

> towny:in-nation=true

#### `towny:town`
Returns name of the nation (if any) at a player's current location

e.g.

> towny:nation=Rome

**config:**
```yaml
towny-nation: true
```

#### `towny:in-wilds`
Returns true if player's location in the wilderness

e.g.

> towny:in-wilds=false

**config:**
```yaml
towny-wilds: true
```
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,23 @@
<version>2.9.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TownyAdvanced</groupId>
<artifactId>Towny</artifactId>
<version>0.96.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>luck-repo</id>
<url>https://repo.lucko.me/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

</project>
6 changes: 6 additions & 0 deletions src/main/java/me/lucko/extracontexts/ExtraContextsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import me.lucko.extracontexts.calculators.HasPlayedBeforeCalculator;
import me.lucko.extracontexts.calculators.PlaceholderApiCalculator;
import me.lucko.extracontexts.calculators.TeamCalculator;
import me.lucko.extracontexts.calculators.TownyNationCalculator;
import me.lucko.extracontexts.calculators.TownyTownCalculator;
import me.lucko.extracontexts.calculators.TownyWildsCalculator;
import me.lucko.extracontexts.calculators.WhitelistedCalculator;
import me.lucko.extracontexts.calculators.WorldGuardFlagCalculator;
import me.lucko.extracontexts.calculators.WorldGuardRegionCalculator;
Expand Down Expand Up @@ -55,6 +58,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

private void setup() {
register("towny-town", "Towny", TownyTownCalculator::new);
register("towny-nation", "Towny", TownyNationCalculator::new);
register("towny-wilds", "Towny", TownyWildsCalculator::new);
register("worldguard-region", "WorldGuard", WorldGuardRegionCalculator::new);
register("worldguard-flag", "WorldGuard", WorldGuardFlagCalculator::new);
register("gamemode", null, GamemodeCalculator::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.lucko.extracontexts.calculators;

import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import net.luckperms.api.context.ContextCalculator;
import net.luckperms.api.context.ContextConsumer;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.context.ImmutableContextSet;
import org.bukkit.entity.Player;

public class TownyNationCalculator implements ContextCalculator<Player> {
private static final String IN_NATION_KEY = "towny:in-nation";
private static final String NATION_KEY = "towny:nation";

private final TownyAPI towny = TownyAPI.getInstance();

@Override
public void calculate(Player player, ContextConsumer contextConsumer) {
final TownBlock townBlock = towny.getTownBlock(player.getLocation());
if (townBlock == null) {
contextConsumer.accept(IN_NATION_KEY, "false");
} else {
if (townBlock.hasTown()) {
try {
final Town town = townBlock.getTown();
if (town.hasNation()) {
contextConsumer.accept(IN_NATION_KEY, "true");
try {
contextConsumer.accept(NATION_KEY, town.getNation().getName());
} catch (NotRegisteredException e) {
e.printStackTrace();
}
} else {
contextConsumer.accept(IN_NATION_KEY, "false");
}
} catch (NotRegisteredException e) {
e.printStackTrace();
}
} else {
contextConsumer.accept(IN_NATION_KEY, "false");
}
}
}

@Override
public ContextSet estimatePotentialContexts() {
final ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
for (String nationKey : towny.getDataSource().getNationsKeys()) {
builder.add(NATION_KEY, nationKey);
}
builder.add(IN_NATION_KEY, "true");
builder.add(IN_NATION_KEY, "false");
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package me.lucko.extracontexts.calculators;

import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.TownBlock;
import net.luckperms.api.context.ContextCalculator;
import net.luckperms.api.context.ContextConsumer;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.context.ImmutableContextSet;
import org.bukkit.entity.Player;

public class TownyTownCalculator implements ContextCalculator<Player> {
private static final String IN_TOWN_KEY = "towny:in-town";
private static final String TOWN_KEY = "towny:town";

private final TownyAPI towny = TownyAPI.getInstance();

@Override
public void calculate(Player player, ContextConsumer contextConsumer) {
final TownBlock townBlock = towny.getTownBlock(player.getLocation());
if (townBlock == null) {
contextConsumer.accept(IN_TOWN_KEY, "false");
} else {
if (townBlock.hasTown()) {
contextConsumer.accept(IN_TOWN_KEY, "true");
try {
contextConsumer.accept(TOWN_KEY, townBlock.getTown().getName());
} catch (NotRegisteredException e) {
e.printStackTrace();
}
} else {
contextConsumer.accept(IN_TOWN_KEY, "false");
}
}
}

@Override
public ContextSet estimatePotentialContexts() {
final ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
for (String townKey : towny.getDataSource().getTownsKeys()) {
builder.add(TOWN_KEY, townKey);
}
builder.add(IN_TOWN_KEY, "true");
builder.add(IN_TOWN_KEY, "false");
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.lucko.extracontexts.calculators;

import com.palmergames.bukkit.towny.TownyAPI;
import net.luckperms.api.context.ContextCalculator;
import net.luckperms.api.context.ContextConsumer;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.context.ImmutableContextSet;
import org.bukkit.entity.Player;

public class TownyWildsCalculator implements ContextCalculator<Player> {
private static final String IN_WILDS_KEY = "towny:in-wilds";

private final TownyAPI towny = TownyAPI.getInstance();

@Override
public void calculate(Player player, ContextConsumer contextConsumer) {
contextConsumer.accept(IN_WILDS_KEY, towny.isWilderness(player.getLocation()) ? "true" : "false");
}

@Override
public ContextSet estimatePotentialContexts() {
final ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
builder.add(IN_WILDS_KEY, "true");
builder.add(IN_WILDS_KEY, "false");
return builder.build();
}
}
17 changes: 17 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,20 @@ has-played-before: false
placeholderapi: false
placeholderapi-placeholders:
allowflight: "%player_allow_flight%"

### Addition: Towny Contexts
# Provides the 'towny:town' and 'towny:in-town' contexts.
# Returns the name of the Town the player is currently in.
#
# e.g. towny:in-town=true and towny:town=townName
towny-town: false
# Provides the 'towny:town' and 'towny:in-nation' contexts.
# Returns the name of the Nation the player is currently in.
#
# e.g. towny:in-nation=true and towny:nation=nationName
towny-nation: false
# Provides the 'towny:in-wilds' context.
# Returns if the player is currently in the wilderness.
#
# e.g. towny:in-wilds=true
towny-wilds: false
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: ${project.version}
author: Luck
main: me.lucko.extracontexts.ExtraContextsPlugin
depend: [LuckPerms]
softdepend: [WorldGuard, PlaceholderAPI]
softdepend: [WorldGuard, PlaceholderAPI, Towny]
api-version: 1.13

commands:
Expand Down