Skip to content

Commit

Permalink
Finished some tasks
Browse files Browse the repository at this point in the history
Atherys#166 | Done
Atherys#125 | Done
Atherys#160 | Done
  • Loading branch information
IkeVoodoo committed Apr 19, 2022
1 parent c6433d8 commit 8ae2b9a
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 29 deletions.
12 changes: 3 additions & 9 deletions src/main/java/com/atherys/towns/command/plot/BordersCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@
@Aliases({"border", "borders"})
@Description("Displays outline for town plots.")
@Permission("atherystowns.plot.border")
public class BordersCommand implements PlayerCommand, ParameterizedCommand {

@Override
public CommandElement[] getArguments() {
return new CommandElement[]{
GenericArguments.bool(Text.of("border"))
};
}
public class BordersCommand implements PlayerCommand {

@Nonnull
@Override
public CommandResult execute(@Nonnull Player source, @Nonnull CommandContext args) throws CommandException {
AtherysTowns.getInstance().getPlotBorderFacade().setPlayerViewBorderStatus(source, args.<Boolean>getOne("border").orElse(false));
AtherysTowns.getInstance().getPlotBorderFacade().setPlayerViewBorderStatus(
source, !AtherysTowns.getInstance().getPlotBorderFacade().isPlayerViewingBorders(source));
return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@
@Aliases("joinable")
@Description("Sets the town joinable status.")
@Permission("atherystowns.town.joinable")
public class SetTownJoinableCommand implements PlayerCommand, ParameterizedCommand {
@Override
public CommandElement[] getArguments() {
return new CommandElement[]{
GenericArguments.bool(Text.of("joinable"))
};
}
public class SetTownJoinableCommand implements PlayerCommand {

@Nonnull
@Override
public CommandResult execute(@Nonnull Player source, @Nonnull CommandContext args) throws CommandException {
AtherysTowns.getInstance().getTownFacade().setPlayerTownJoinable(
source, args.<Boolean>getOne("joinable").get()
source, !AtherysTowns.getInstance().getTownFacade().getPlayerTown(source).isFreelyJoinable()
);
return CommandResult.success();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.atherys.towns.command.town;

import com.atherys.core.command.PlayerCommand;
import com.atherys.core.command.annotation.Aliases;
import com.atherys.core.command.annotation.Description;
import com.atherys.core.command.annotation.Permission;
import com.atherys.towns.AtherysTowns;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.entity.living.player.Player;

import javax.annotation.Nonnull;

@Aliases("mobs")
@Description("Enables/Disables Hostile Mob Spawning in your Town.")
@Permission("atherystowns.town.mobs")
public class SetTownMobsCommand implements PlayerCommand {

@Nonnull
@Override
public CommandResult execute(@Nonnull Player source, @Nonnull CommandContext args) throws CommandException {
AtherysTowns.getInstance().getTownFacade().setPlayerTownMobs(source,
!AtherysTowns.getInstance().getTownFacade().getPlayerTown(source).isMobs());
return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,19 @@
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;

import javax.annotation.Nonnull;

@Aliases("pvp")
@Description("Enables/Disables PvP Protection in your Town.")
@Permission("atherystowns.town.pvp")
public class SetTownPvpCommand implements PlayerCommand, ParameterizedCommand {
@Override
public CommandElement[] getArguments() {
return new CommandElement[]{
GenericArguments.bool(Text.of("pvp"))
};
}

public class SetTownPvpCommand implements PlayerCommand {
@Nonnull
@Override
public CommandResult execute(@Nonnull Player source, @Nonnull CommandContext args) throws CommandException {
AtherysTowns.getInstance().getTownFacade().setPlayerTownPvp(source, args.<Boolean>getOne("pvp").orElse(false));
AtherysTowns.getInstance().getTownFacade().setPlayerTownPvp(source,
!AtherysTowns.getInstance().getTownFacade().getPlayerTown(source).isPvpEnabled());
return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
SetTownMotdCommand.class,
SetTownNameCommand.class,
SetTownJoinableCommand.class,
SetTownMobsCommand.class,
SetTownPvpCommand.class,
InviteToTownCommand.class,
TownKickCommand.class,
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/atherys/towns/facade/PlotFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static org.spongepowered.api.text.Text.NEW_LINE;
Expand Down Expand Up @@ -111,6 +112,14 @@ public TownPlot getPlotAtPlayer(Player player) throws TownsCommandException {
new TownsCommandException("You are not standing in a plot."));
}

public boolean checkHostileSpawningAtLocation(Location<World> location) {
AtomicBoolean spawn = new AtomicBoolean(true);

plotService.getTownPlotByLocation(location).ifPresent(plot -> spawn.set(plot.getTown().isMobs()));

return spawn.get();
}

public Set<TownsPermissionContext> getRelevantResidentContexts(TownPlot plot, Resident resident) {
Set<TownsPermissionContext> contexts = new HashSet<>();
contexts.add(TownsPermissionContexts.ALL);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/atherys/towns/facade/ResidentFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ public void sendResidentInfo(MessageReceiver receiver, Resident resident) {
.map(formatter::format)
.orElse("N/A");

String firstJoined = UserUtils.getUser(resident.getId())
.flatMap(user -> user.get(Keys.FIRST_DATE_PLAYED))
.map(formatter::format)
.orElse("N/A");

Text.Builder residentInfo = Text.builder()
.append(Text.of(DARK_GRAY, "[]", padding, "[ ", title, DARK_GRAY, " ]", padding, "[]", Text.NEW_LINE))
.append(Text.of(DARK_GREEN, "Town: ", townFacade.renderTown(resident.getTown()), Text.NEW_LINE))
.append(Text.of(DARK_GREEN, "Last online: ", GOLD, lastPlayed, Text.NEW_LINE))
.append(Text.of(DARK_GREEN, "First online: ", GOLD, formatter.format(resident.getRegisteredOn()), Text.NEW_LINE))
.append(Text.of(DARK_GREEN, "First online: ", GOLD, firstJoined, Text.NEW_LINE))
.append(townsMsg.renderBank(resident.getId()));


Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/atherys/towns/facade/TownFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public void setPlayerTownJoinable(Player player, boolean joinable) throws TownsC
townsMsg.info(player, "Your town is now ", joinable ? "freely joinable." : "not freely joinable.");
}

public void setPlayerTownMobs(Player playerb, boolean mobs) throws TownsCommandException {
Town town = getPlayerTown(playerb);

townService.setTownMobs(town, mobs);
townsMsg.info(playerb, "Your town now has hostile mobs ", mobs ? "enabled." : "disabled.");
}

public void ruinPlayerTown(Player player) throws TownsCommandException {
Town town = getPlayerTown(player);
Resident resident = residentService.getOrCreate(player);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/atherys/towns/listener/ProtectionListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.Hostile;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.event.Listener;
Expand All @@ -16,6 +17,7 @@
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.entity.CollideEntityEvent;
import org.spongepowered.api.event.entity.InteractEntityEvent;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.event.filter.cause.Root;

public class ProtectionListener {
Expand Down Expand Up @@ -128,4 +130,14 @@ public void onEntityDamage(CollideEntityEvent.Impact event) {
});
}

@Listener
public void onEntitySpawn(SpawnEntityEvent event) {
event.filterEntities(entity -> {
if(entity instanceof Hostile)
return plotFacade.checkHostileSpawningAtLocation(entity.getLocation());

return true;
});
}

}
1 change: 1 addition & 0 deletions src/main/java/com/atherys/towns/model/entity/Resident.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/atherys/towns/model/entity/Town.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class Town implements Identifiable<Long> {

private boolean pvpEnabled;

private boolean hostileMobsAllowed;

private UUID bank;

private LocalDateTime lastTaxDate;
Expand Down Expand Up @@ -199,6 +201,14 @@ public void setPvpEnabled(boolean pvpEnabled) {
this.pvpEnabled = pvpEnabled;
}

public void setMobs(boolean mobs) {
this.hostileMobsAllowed = mobs;
}

public boolean isMobs() {
return hostileMobsAllowed;
}

public TextColor getColor() {
return color;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/atherys/towns/service/TownService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class TownService {

public static final boolean DEFAULT_TOWN_FREELY_JOINABLE = false;

public static final boolean DEFAULT_HOSTILE_MOBS = true;

private final TownsConfig config;

private final PlotService plotService;
Expand Down Expand Up @@ -102,6 +104,7 @@ public Town createTown(Player leader, TownPlot homePlot, String name, @Nullable
town.setMaxSize(config.TOWN.DEFAULT_TOWN_MAX_SIZE);
town.setPvpEnabled(DEFAULT_TOWN_PVP);
town.setFreelyJoinable(DEFAULT_TOWN_FREELY_JOINABLE);
town.setMobs(DEFAULT_HOSTILE_MOBS);
town.setWorld(leader.getWorld().getUniqueId());
town.setLastTaxDate(LocalDateTime.now());
town.setTaxFailedCount(0);
Expand Down Expand Up @@ -183,6 +186,11 @@ public void setTownPvp(Town town, boolean pvp) {
townRepository.saveOne(town);
}

public void setTownMobs(Town town, boolean hostileMobs) {
town.setMobs(hostileMobs);
townRepository.saveOne(town);
}

public void setTownTaxFailCount(Town town, int count) {
town.setTaxFailedCount(count);
townRepository.saveOne(town);
Expand Down

0 comments on commit 8ae2b9a

Please sign in to comment.