Skip to content

Commit

Permalink
Split user and admin commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePixelbrain committed Jun 16, 2023
1 parent 19b3499 commit d345a84
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 148 deletions.
17 changes: 9 additions & 8 deletions src/main/java/com/cricketcraft/ftbisland/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class Config {

private static Configuration config;
public static int maxIslandsPerPlayer = 1;

public static void init(File file) {
if (config == null) {
Expand All @@ -20,14 +21,6 @@ public static void init(File file) {
}

private static void loadConfig() {
FTBIslands.maxIslands = config.getInt(
"Max Islands",
"misc",
100,
1,
1000,
"The maximum amount of islands that can be created. This number will be multiplied by four."
+ " Be careful with high numbers.");
if (!config.hasKey("misc", "Island Type")) {
boolean skyFactory = config
.getBoolean("Sky Factory", "misc", false, "Set this to true if you are playing on Sky Factory.");
Expand Down Expand Up @@ -77,6 +70,14 @@ private static void loadConfig() {
}
}

maxIslandsPerPlayer = config.getInt(
"Max Islands Per Player",
"misc",
maxIslandsPerPlayer,
0,
1000,
"Maximum amount of islands a player can own");

if (config.hasChanged()) {
config.save();
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/cricketcraft/ftbisland/FTBIslands.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import org.apache.logging.log4j.Logger;

import com.cricketcraft.ftbisland.commands.*;
import com.cricketcraft.ftbisland.commands.admin.AdminCreateCommand;
import com.cricketcraft.ftbisland.commands.admin.AdminDeleteCommand;
import com.cricketcraft.ftbisland.commands.admin.AdminRenameCommand;
import com.cricketcraft.ftbisland.commands.admin.AdminTeleportCommand;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
Expand All @@ -20,7 +24,6 @@
acceptableRemoteVersions = "*")
public class FTBIslands {

public static int maxIslands;
public static Logger logger;
public static String islandType;
public static LMMod mod;
Expand All @@ -33,10 +36,13 @@ public void serverLoading(FMLServerStartingEvent event) {
logger.info("Registering commands.");
event.registerServerCommand(new CreateIslandCommand());
event.registerServerCommand(new DeleteIslandCommand());
event.registerServerCommand(new JoinIslandCommand());
event.registerServerCommand(new TeleportIslandCommand());
event.registerServerCommand(new ListIslandCommand());
event.registerServerCommand(new RenameIslandCommand());
event.registerServerCommand(new TeleportIslandCommand());
event.registerServerCommand(new AdminCreateCommand());
event.registerServerCommand(new AdminDeleteCommand());
event.registerServerCommand(new AdminRenameCommand());
event.registerServerCommand(new AdminTeleportCommand());
logger.info("Finished registering commands.");
islandStorage.reloadContainer();
}
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/cricketcraft/ftbisland/IslandCreator.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.cricketcraft.ftbisland;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.tileentity.TileEntitySign;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import com.cricketcraft.ftbisland.model.Island;
import com.cricketcraft.ftbisland.model.IslandContainer;
Expand All @@ -20,7 +19,8 @@ public class IslandCreator {

private static final Item chickenStick = GameRegistry.findItem("excompressum", "chickenStick");

public static void spawnIslandAt(World world, int x, int y, int z, String islandName, EntityPlayer owner) {
public static void spawnIslandAt(int x, int y, int z, String islandName, EntityPlayerMP owner) {
World world = owner.worldObj;
if (FTBIslands.islandType.equalsIgnoreCase("tree")) {
world.setBlock(x, y, z, Blocks.grass);
for (int c = -3; c < 2; c++) {
Expand Down Expand Up @@ -76,16 +76,17 @@ public static void spawnIslandAt(World world, int x, int y, int z, String island
}
}
} else {
for (int c = 0; c < 3; c++) {
for (int d = 0; d < 3; d++) {
for (int c = -1; c < 2; c++) {
for (int d = -1; d < 2; d++) {
world.setBlock(x + c, y, z + d, Blocks.dirt);
}
}

world.setBlock(x + 2, y + 1, z + 1, Blocks.chest);
world.getBlock(x + 2, y + 1, z + 1)
.rotateBlock(world, x + 2, y + 1, z + 1, ForgeDirection.WEST);
TileEntityChest chest = (TileEntityChest) world.getTileEntity(x + 2, y + 1, z + 1);
int chestX = x + 1;
int chestY = y + 1;
int chestZ = z;
world.setBlock(chestX, chestY, chestZ, Blocks.chest, 4, 3);
TileEntityChest chest = (TileEntityChest) world.getTileEntity(chestX, chestY, chestZ);

chest.setInventorySlotContents(0, new ItemStack(Blocks.flowing_water, 1));
chest.setInventorySlotContents(1, new ItemStack(Blocks.flowing_lava, 1));
Expand All @@ -105,13 +106,9 @@ public static void spawnIslandAt(World world, int x, int y, int z, String island
IslandContainer container = FTBIslands.getIslandStorage()
.getContainer();
int dim = owner.worldObj.provider.dimensionId;
int offsetY = FTBIslands.islandType.equalsIgnoreCase("tree") ? 7 : 1;
container.getIslands()
.add(
new Island(
islandName,
owner.getGameProfile()
.getId(),
new Island.Position(x, y, z, dim)));
.add(new Island(islandName, owner.getUniqueID(), new Island.Position(x, y + offsetY, z, dim)));
container.incrementIslandsCreated(dim);
FTBIslands.getIslandStorage()
.saveContainer();
Expand Down
124 changes: 88 additions & 36 deletions src/main/java/com/cricketcraft/ftbisland/IslandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,126 @@

import java.util.*;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.World;

import org.apache.commons.lang3.tuple.Pair;

import com.cricketcraft.ftbisland.model.Island;

public class IslandUtils {

public static void createIsland(World world, String islandName, EntityPlayer owner) {
public enum StatusCode {

SUCCESS("Success!"),
FAIL_EXIST("Island %s already exists!"),
FAIL_NOT_EXIST("Island %s doesn't exist!"),
FAIL_MAX_ISLANDS("A player can only have %d island(s)!"),
FAIL_WRONG_OWNER("Island %s is owned by a different player!");

private final String message;
private Object[] args;

StatusCode(String message) {
this.message = message;
}

public StatusCode setArgs(Object... args) {
this.args = args;
return this;
}

public String getMessage() {
return String.format(message, args);
}
}

public static StatusCode createIsland(String islandName, EntityPlayerMP owner, boolean adminSender) {
FTBIslands.getIslandStorage()
.reloadContainer();
if (FTBIslands.getIslandStorage()
.getContainer()
.doesIslandExist(islandName)) {
return StatusCode.FAIL_EXIST.setArgs(islandName);
}
if (!adminSender && FTBIslands.getIslandStorage()
.getContainer()
.islandsOwnedByUser(owner.getUniqueID()) >= Config.maxIslandsPerPlayer) {
return StatusCode.FAIL_MAX_ISLANDS.setArgs(Config.maxIslandsPerPlayer);
}
Pair<Integer, Integer> spiralLoc = calculateSpiral(
FTBIslands.getIslandStorage()
.getContainer()
.getIslandsCreated(owner.worldObj.provider.dimensionId));
IslandCreator.spawnIslandAt(
world,
spiralLoc.getLeft() * 512 + 256,
64,
spiralLoc.getRight() * 512 + 256,
islandName,
owner);
IslandCreator
.spawnIslandAt(spiralLoc.getLeft() * 512 + 256, 64, spiralLoc.getRight() * 512 + 256, islandName, owner);
return StatusCode.SUCCESS;
}

public static void renameIsland(Island island, String newName) {
island.setName(newName);
public static StatusCode renameIsland(String oldName, String newName, UUID owner, boolean adminSender) {
FTBIslands.getIslandStorage()
.reloadContainer();
Optional<Island> island = FTBIslands.getIslandStorage()
.getContainer()
.getIsland(oldName);
if (island.isEmpty()) {
return StatusCode.FAIL_NOT_EXIST.setArgs(oldName);
}
if (!adminSender && !island.get()
.getOwner()
.equals(owner)) {
return StatusCode.FAIL_WRONG_OWNER.setArgs(oldName);
}
if (FTBIslands.getIslandStorage()
.getContainer()
.doesIslandExist(newName)) {
return StatusCode.FAIL_EXIST.setArgs(newName);
}
island.get()
.setName(newName);
FTBIslands.getIslandStorage()
.saveContainer();
return StatusCode.SUCCESS;
}

public static void joinIsland(String islandName, EntityPlayer player) {
public static StatusCode teleportToIsland(String islandName, EntityPlayerMP player) {
FTBIslands.getIslandStorage()
.reloadContainer();
Optional<Island> island = FTBIslands.getIslandStorage()
.getContainer()
.getIslandByName(islandName);
if (island.isPresent()) {
Island.Position pos = island.get()
.getPos();
if (player.dimension != pos.getDim()) {
player.travelToDimension(pos.getDim());
}
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
int height = FTBIslands.islandType.equalsIgnoreCase("tree") ? 6 : 2;
double xAndZ = FTBIslands.islandType.equalsIgnoreCase("grass") ? 0.5 : 1.5;
if (player instanceof EntityPlayerMP) {
EntityPlayerMP playerMP = (EntityPlayerMP) player;
playerMP.setPositionAndUpdate(x + xAndZ, y + height, z + xAndZ);
// ChunkCoordinates chunk = new ChunkCoordinates(x, y, z);
// playerMP.setSpawnChunk(chunk, true);
}
} else {
player.addChatComponentMessage(new ChatComponentText("Island does not exist!"));
.getIsland(islandName);
if (island.isEmpty()) {
return StatusCode.FAIL_NOT_EXIST.setArgs(islandName);
}
Island.Position pos = island.get()
.getPos();
if (player.dimension != pos.getDim()) {
player.travelToDimension(pos.getDim());
}
player.setPositionAndUpdate(pos.getX() + .5, pos.getY(), pos.getZ() + .5);
return StatusCode.SUCCESS;
}

public static void deleteIsland(Island island) {
public static StatusCode deleteIsland(String islandName, UUID owner, boolean adminSender) {
FTBIslands.getIslandStorage()
.reloadContainer();
Optional<Island> island = FTBIslands.getIslandStorage()
.getContainer()
.getIsland(islandName);
if (island.isEmpty()) {
return StatusCode.FAIL_NOT_EXIST.setArgs(islandName);
}
if (!adminSender && !island.get()
.getOwner()
.equals(owner)) {
return StatusCode.FAIL_WRONG_OWNER.setArgs(islandName);
}
FTBIslands.getIslandStorage()
.getContainer()
.getIslands()
.remove(island);
.remove(island.get());
FTBIslands.getIslandStorage()
.saveContainer();
return StatusCode.SUCCESS;
}

private static Pair<Integer, Integer> calculateSpiral(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;

import com.cricketcraft.ftbisland.FTBIslands;
import com.cricketcraft.ftbisland.IslandUtils;

import ftb.lib.api.cmd.CommandLM;
Expand All @@ -13,7 +13,7 @@
public class CreateIslandCommand extends CommandLM {

public CreateIslandCommand() {
super("island_create", CommandLevel.OP);
super("island_create", CommandLevel.ALL);
}

@Override
Expand All @@ -24,14 +24,11 @@ public String getCommandUsage(ICommandSender ics) {
@Override
public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException {
checkArgs(strings, 1);
FTBIslands.getIslandStorage()
.reloadContainer();
if (FTBIslands.getIslandStorage()
.getContainer()
.doesIslandExist(strings[0])) {
return error(FTBIslands.mod.chatComponent("cmd.create_exists", strings[0]));
IslandUtils.StatusCode status = IslandUtils
.createIsland(strings[0], getCommandSenderAsPlayer(iCommandSender), false);
if (status != IslandUtils.StatusCode.SUCCESS) {
return error(new ChatComponentText(status.getMessage()));
}
IslandUtils.createIsland(iCommandSender.getEntityWorld(), strings[0], getCommandSenderAsPlayer(iCommandSender));
return FTBIslands.mod.chatComponent("cmd.create_success", strings[0]);
return new ChatComponentText(String.format("Island %s successfully created", strings[0]));
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.cricketcraft.ftbisland.commands;

import java.util.Optional;
import java.util.UUID;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;

import com.cricketcraft.ftbisland.FTBIslands;
import com.cricketcraft.ftbisland.IslandUtils;
import com.cricketcraft.ftbisland.model.Island;

import ftb.lib.api.cmd.CommandLM;
import ftb.lib.api.cmd.CommandLevel;

public class DeleteIslandCommand extends CommandLM {

public DeleteIslandCommand() {
super("island_delete", CommandLevel.OP);
super("island_delete", CommandLevel.ALL);
}

@Override
Expand All @@ -26,23 +26,20 @@ public String getCommandUsage(ICommandSender ics) {

@Override
public String[] getTabStrings(ICommandSender ics, String[] args, int i) throws CommandException {
UUID uuid = getCommandSenderAsPlayer(ics).getUniqueID();
return i == 0 ? FTBIslands.getIslandStorage()
.getContainer()
.getAllIslandNames() : null;
.getIslandNames(uuid) : null;
}

@Override
public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException {
checkArgs(strings, 1);
FTBIslands.getIslandStorage()
.reloadContainer();
Optional<Island> island = FTBIslands.getIslandStorage()
.getContainer()
.getIslandByName(strings[0]);
if (!island.isPresent()) {
return error(FTBIslands.mod.chatComponent("cmd.delete_not_exist", strings[0]));
UUID uuid = getCommandSenderAsPlayer(iCommandSender).getUniqueID();
IslandUtils.StatusCode status = IslandUtils.deleteIsland(strings[0], uuid, false);
if (status != IslandUtils.StatusCode.SUCCESS) {
return error(new ChatComponentText(status.getMessage()));
}
IslandUtils.deleteIsland(island.get());
return FTBIslands.mod.chatComponent("cmd.delete_success", strings[0]);
return new ChatComponentText(String.format("Successfully deleted %s", strings[0]));
}
}
Loading

0 comments on commit d345a84

Please sign in to comment.