Skip to content

Commit

Permalink
Update rep command to allow OP players to set rep of other players #100
Browse files Browse the repository at this point in the history
  • Loading branch information
frodare committed Jan 31, 2017
1 parent 9cce171 commit a687e02
Showing 1 changed file with 64 additions and 42 deletions.
106 changes: 64 additions & 42 deletions java/net/torocraft/toroquest/ToroQuestCommand.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package net.torocraft.toroquest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.torocraft.toroquest.civilization.CivilizationType;
import net.torocraft.toroquest.civilization.CivilizationUtil;
import net.torocraft.toroquest.civilization.CivilizationsWorldSaveData;
import net.torocraft.toroquest.civilization.Province;
Expand All @@ -30,6 +33,7 @@
import net.torocraft.toroquest.util.BookCreator;
import net.torocraft.toroquest.util.BookCreator.BookTypes;


public class ToroQuestCommand extends CommandBase {

@Override
Expand Down Expand Up @@ -65,7 +69,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
System.out.println("command " + command);

if ("rep".equals(command)) {
adjustRep(player, args);
adjustRep(server, player, args);
} else if ("list".equals(command)) {
listCommand(player, args);
} else if ("gen".equals(command)) {
Expand All @@ -80,14 +84,13 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
throw new WrongUsageException("commands.tq.usage", new Object[0]);
}
}

private void bookCommand(EntityPlayer player, String[] args) throws CommandException {
List<ItemStack> items = new ArrayList<ItemStack>();
items.add(BookCreator.createBook(BookTypes.CIV_LORE, "pantheon_unbiased_book_1"));
dropItems(player, items);
}


private void listCommand(EntityPlayer player, String[] args) throws CommandException {
List<Province> provinces = CivilizationsWorldSaveData.get(player.world).getProvinces();
StringBuilder sb = new StringBuilder();
Expand All @@ -97,7 +100,7 @@ private void listCommand(EntityPlayer player, String[] args) throws CommandExcep
player.sendMessage(new TextComponentString(sb.toString()));
}

private void adjustRep(EntityPlayer player, String[] args) throws CommandException {
private void adjustRep(MinecraftServer server, EntityPlayer player, String[] args) throws CommandException {
int amount;
if (args.length < 2) {
throw new WrongUsageException("commands.tq.usage", new Object[0]);
Expand All @@ -108,11 +111,30 @@ private void adjustRep(EntityPlayer player, String[] args) throws CommandExcepti
throw new WrongUsageException("commands.tq.usage", new Object[0], e);
}
}
Province province = CivilizationUtil.getProvinceAt(player.getEntityWorld(), player.chunkCoordX, player.chunkCoordZ);
if (province == null || province.civilization == null) {
throw new WrongUsageException("commands.tq.not_in_civ", new Object[0]);

Entity entity = null;
EntityPlayer playerToSet = player;

if (args.length >= 3) {
entity = getEntity(server, player, args[2]);
}
PlayerCivilizationCapabilityImpl.get(player).setReputation(province.civilization, amount);

if (entity != null && entity instanceof EntityPlayer) {
playerToSet = (EntityPlayer) entity;
}

CivilizationType civ;
if (args.length >= 4) {
civ = CivilizationType.valueOf(args[3]);
} else {
Province province = CivilizationUtil.getProvinceAt(player.getEntityWorld(), player.chunkCoordX, player.chunkCoordZ);
if (province == null || province.civilization == null) {
throw new WrongUsageException("commands.tq.not_in_civ", new Object[0]);
}
civ = province.civilization;
}

PlayerCivilizationCapabilityImpl.get(playerToSet).setReputation(civ, amount);
}

private void genCommand(EntityPlayer player, String[] args) throws CommandException {
Expand Down Expand Up @@ -157,7 +179,8 @@ private void guiCommand(EntityPlayer player, String[] args) throws CommandExcept
String type = args[1];

if ("lord".equals(type)) {
player.openGui(ToroQuest.INSTANCE, VillageLordGuiHandler.getGuiID(), player.world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ());
player.openGui(ToroQuest.INSTANCE, VillageLordGuiHandler.getGuiID(), player.world, player.getPosition().getX(),
player.getPosition().getY(), player.getPosition().getZ());
return;
}

Expand Down Expand Up @@ -230,7 +253,7 @@ private void questCommand(EntityPlayer player, String[] args) throws CommandExce
player.sendMessage(new TextComponentString(quest.getDescription()));
dropItems(player, returnItems);
}

} else if ("complete".equals(sub)) {

QuestData data = PlayerCivilizationCapabilityImpl.get(player).getCurrentQuestFor(province);
Expand Down Expand Up @@ -304,47 +327,46 @@ private void dropItems(EntityPlayer player, List<ItemStack> items) {

@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos pos) {
List<String> tabOptions = new ArrayList<String>();

if (args.length == 0) {
tabOptions.add("tq");
return l("tq");
} else if (args.length == 1) {
String command = args[0];

if (command == null || command.trim().length() == 0) {
tabOptions.add("rep");
tabOptions.add("gen");
tabOptions.add("gui");
tabOptions.add("quest");
tabOptions.add("list");
tabOptions.add("book");
} else {
if (command.startsWith("r")) {
tabOptions.add("rep");
} else if (command.startsWith("g")) {
tabOptions.add("gen");
tabOptions.add("gui");
}
}
return getListOfStringsMatchingLastWord(args, "rep", "gen", "gui", "quest", "list", "book");

} else if (args.length == 2) {
if ("gen".equals(args[0])) {
tabOptions.add("throne_room");
tabOptions.add("mage_tower");
tabOptions.add("bastions_lair");
tabOptions.add("monolith");
tabOptions.add("graveyard");

return getListOfStringsMatchingLastWord(args, "throne_room", "mage_tower", "bastions_lair", "monolith", "graveyard");

} else if ("gui".equals(args[0])) {
tabOptions.add("lord");
return getListOfStringsMatchingLastWord(args, "lord");

} else if ("quest".equals(args[0])) {
tabOptions.add("current");
tabOptions.add("complete");
tabOptions.add("list");
tabOptions.add("next");
tabOptions.add("accept");
tabOptions.add("reject");

return getListOfStringsMatchingLastWord(args, "current", "complete", "list", "next", "accept", "reject");

} else if ("rep".equals(args[0])) {
return l("10");
}
} else if (args.length == 3) {
if ("rep".equals(args[0])) {
return getListOfStringsMatchingLastWord(args, server.getOnlinePlayerNames());
}
} else if (args.length == 4) {
if ("rep".equals(args[0])) {
return getListOfStringsMatchingLastWord(args, Arrays.asList(CivilizationType.values()));
}
}

return l();
}

private <T> List<T> l(T... items) {
List<T> l = new ArrayList<T>();
for (T item : items) {
l.add(item);
}
return tabOptions;
return l;
}

}

0 comments on commit a687e02

Please sign in to comment.