Skip to content

Commit

Permalink
Added public mode. Fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Oct 17, 2016
1 parent 1430cb9 commit f35e93e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 26 deletions.
11 changes: 5 additions & 6 deletions src/main/java/net/doubledoordev/inventorylock/asm/Hooks.java
Expand Up @@ -32,6 +32,7 @@
import java.util.UUID;

import static net.doubledoordev.inventorylock.util.Constants.MOD_ID;
import static net.doubledoordev.inventorylock.util.Constants.PUBLIC_KEY;

/**
* @author Dries007
Expand All @@ -46,13 +47,11 @@ public static LockCode fromNBT(NBTTagCompound nbt)
{
if (nbt.hasKey(MOD_ID, net.minecraftforge.common.util.Constants.NBT.TAG_LIST))
{
BetterLockCode betterLockCode = new BetterLockCode();
BetterLockCode blc = new BetterLockCode();
NBTTagList list = nbt.getTagList(MOD_ID, net.minecraftforge.common.util.Constants.NBT.TAG_STRING);
for (int i = 0; i < list.tagCount(); i++)
{
betterLockCode.add(UUID.fromString(list.getStringTagAt(i)));
}
return betterLockCode;
for (int i = 0; i < list.tagCount(); i++) blc.add(UUID.fromString(list.getStringTagAt(i)));
blc.setPublic(nbt.getBoolean(PUBLIC_KEY));
return blc;
}
return LockCode.fromNBT(nbt);
}
Expand Down
Expand Up @@ -49,7 +49,6 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -58,7 +57,7 @@
public class ClientEventHandler
{
public static final ClientEventHandler CLIENT_EVENT_HANDLER = new ClientEventHandler();
public static final Cache<BlockPos, Set<String>> LOCK_CACHE = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build();
public static final Cache<BlockPos, Reply> LOCK_CACHE = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build();

private ClientEventHandler()
{
Expand Down Expand Up @@ -93,21 +92,21 @@ public void onRenderGameOverlayText(RenderGameOverlayEvent.Text event)
right.add(TextFormatting.GREEN + "Lockable!");
right.add("This info updates every 5 seconds.");

Set<String> list = LOCK_CACHE.getIfPresent(blockpos);
if (list == null)
Reply reply = LOCK_CACHE.getIfPresent(blockpos);
if (reply == null)
{
right.add("No information (yet)...");
return;
}
if (list.isEmpty())
if (reply.value.isEmpty())
{
right.add(TextFormatting.GOLD + "Unlocked");
return;
}
right.add(TextFormatting.GOLD + "Locked");
right.add(list.contains(mc.thePlayer.getName()) ? TextFormatting.GREEN + "You have access!" : TextFormatting.RED + "You do not have access.");
right.add(reply.pub ? TextFormatting.GOLD + "Public" : (reply.value.contains(mc.thePlayer.getName()) ? TextFormatting.GREEN + "You have access!" : TextFormatting.RED + "You do not have access."));
right.add("List of people with access:");
right.addAll(list);
right.addAll(reply.value);
}
}

Expand All @@ -133,7 +132,7 @@ public static class Handler implements IMessageHandler<Reply, IMessage>
@Override
public IMessage onMessage(Reply message, MessageContext ctx)
{
LOCK_CACHE.put(message.key, message.value);
LOCK_CACHE.put(message.key, message);
return null;
}
}
Expand Down
Expand Up @@ -24,6 +24,7 @@

package net.doubledoordev.inventorylock.cmd;

import com.google.common.collect.ImmutableList;
import com.mojang.authlib.GameProfile;
import net.doubledoordev.inventorylock.InventoryLock;
import net.doubledoordev.inventorylock.util.Action;
Expand Down Expand Up @@ -56,6 +57,12 @@ public String getCommandName()
return "inventorylock";
}

@Override
public List<String> getCommandAliases()
{
return ImmutableList.of("invlock");
}

@Override
public boolean checkPermission(MinecraftServer server, ICommandSender sender)
{
Expand Down Expand Up @@ -94,6 +101,11 @@ else if (args[0].equalsIgnoreCase("inspect"))
Wand.from(player, EnumHand.MAIN_HAND).setDisplayName("Inspect wand").setAction(Action.INSPECT);
player.addChatComponentMessage(new TextComponentString("You are now holding a Inspect wand.").setStyle(new Style().setColor(TextFormatting.AQUA)));
}
else if (args[0].equalsIgnoreCase("public"))
{
Wand.from(player, EnumHand.MAIN_HAND).setDisplayName("Public wand").setAction(Action.PUBLIC);
player.addChatComponentMessage(new TextComponentString("You are now holding a Public wand.").setStyle(new Style().setColor(TextFormatting.AQUA)));
}
else if (args[0].equalsIgnoreCase("add")) doAdd(server, player, args);
else if (args[0].equalsIgnoreCase("remove")) doRemove(server, player, args);
else displayHelp(player);
Expand All @@ -102,7 +114,7 @@ else if (args[0].equalsIgnoreCase("inspect"))
@Override
public List<String> getTabCompletionOptions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos pos)
{
if (args.length == 1) return getListOfStringsMatchingLastWord(args, "help", "list", "lock", "unlock", "clone", "inspect", "add", "remove");
if (args.length == 1) return getListOfStringsMatchingLastWord(args, "help", "list", "lock", "unlock", "clone", "inspect", "public", "add", "remove");
if (args.length > 1 && (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove"))) return getListOfStringsMatchingLastWord(args, server.getAllUsernames());
return super.getTabCompletionOptions(server, sender, args, pos);
}
Expand All @@ -118,6 +130,7 @@ private void displayHelp(EntityPlayerMP sender) throws CommandException
"- unlock: Create a 'Unlock wand' from the held item.",
"- clone: Copy a wand from primary to secondary hand.",
"- inspect: Create a 'Inspect wand' from the held item.",
"- public: Create a 'Public wand' from the held item.",
TextFormatting.AQUA + "If you are holding an existing Add or Remove wand:",
"- add [name or UUID] ... : Add names to the UUID list.",
"- remove [name or UUID] ... : Remove names from the UUID list.",
Expand Down
Expand Up @@ -43,6 +43,7 @@
public class Reply implements IMessage
{
public BlockPos key;
public boolean pub;
public Set<String> value = new LinkedHashSet<String>();

public Reply(BlockPos key, BetterLockCode blc)
Expand All @@ -55,6 +56,7 @@ public Reply(BlockPos key, BetterLockCode blc)
if (gp == null) value.add(uuid.toString());
else value.add(gp.getName());
}
pub = blc.isPublic();
}

@SuppressWarnings("unused")
Expand All @@ -68,6 +70,7 @@ public void fromBytes(ByteBuf buf)
key = BlockPos.fromLong(buf.readLong());
int i = buf.readInt();
while (i-- > 0) value.add(ByteBufUtils.readUTF8String(buf));
pub = buf.readBoolean();
}

@Override
Expand All @@ -76,5 +79,6 @@ public void toBytes(ByteBuf buf)
buf.writeLong(key.toLong());
buf.writeInt(value.size());
for (String name : value) ByteBufUtils.writeUTF8String(buf, name);
buf.writeBoolean(pub);
}
}
Expand Up @@ -34,8 +34,6 @@
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import java.util.UUID;

/**
* @author Dries007
*/
Expand Down
Expand Up @@ -150,6 +150,12 @@ public void onPlayerInteractRightClickBlock(PlayerInteractEvent.RightClickBlock
player.addChatComponentMessage(new TextComponentString("Unlocked!").setStyle(new Style().setColor(TextFormatting.GREEN)));
return;
}
else if (action == Action.PUBLIC) // We want to make public (set back to the EMPTY_CODE singleton)
{
blc.setPublic(!blc.isPublic());
player.addChatComponentMessage(new TextComponentString("Unlocked!").setStyle(new Style().setColor(TextFormatting.GREEN)));
return;
}
else if (action == Action.ADD) // We want to add uuids
{
NBTTagList list = nbt.getTagList(UUIDS, TAG_STRING);
Expand All @@ -170,7 +176,8 @@ else if (action == Action.REMOVE) // We want to remove uuids

private void printList(EntityPlayer player, BetterLockCode blc)
{
player.addChatComponentMessage(new TextComponentString("People with access:").setStyle(new Style().setColor(TextFormatting.AQUA)));
if (blc.isPublic()) player.addChatComponentMessage(new TextComponentString("Public chest, but owned by:").setStyle(new Style().setColor(TextFormatting.AQUA)));
else player.addChatComponentMessage(new TextComponentString("People with access:").setStyle(new Style().setColor(TextFormatting.AQUA)));
for (UUID uuid : blc.list)
{
//noinspection ConstantConditions
Expand Down
Expand Up @@ -29,7 +29,7 @@
*/
public enum Action
{
NONE(false), LOCK(false), UNLOCK(false), ADD(true), REMOVE(true), INSPECT(false);
NONE(false), LOCK(false), UNLOCK(false), ADD(true), REMOVE(true), INSPECT(false), PUBLIC(false);

public final boolean hasUUIDs;

Expand Down
Expand Up @@ -39,13 +39,15 @@
import java.util.UUID;

import static net.doubledoordev.inventorylock.util.Constants.MOD_ID;
import static net.doubledoordev.inventorylock.util.Constants.PUBLIC_KEY;

/**
* @author Dries007
*/
public class BetterLockCode extends LockCode
{
public final Set<UUID> list = new LinkedHashSet<UUID>();
private boolean pub;

public BetterLockCode()
{
Expand All @@ -66,18 +68,14 @@ public BetterLockCode remove(UUID uuid)

public boolean contains(EntityPlayer player)
{
if (pub) return true;
MinecraftServer server = player.getServer();
if (server != null && server.getPlayerList().getOppedPlayers().getPermissionLevel(player.getGameProfile()) > 0)
{
if (!contains(player.getUniqueID())) player.addChatComponentMessage(new TextComponentString("OP Bypass").setStyle(new Style().setColor(TextFormatting.GRAY)));
if (!list.contains(player.getUniqueID())) player.addChatComponentMessage(new TextComponentString("OP Bypass").setStyle(new Style().setColor(TextFormatting.GRAY)));
return true;
}
return contains(player.getUniqueID());
}

public boolean contains(UUID uuid)
{
return list.contains(uuid);
return list.contains(player.getUniqueID());
}

@Override
Expand All @@ -92,5 +90,16 @@ public void toNBT(NBTTagCompound nbt)
NBTTagList list = new NBTTagList();
for (UUID uuid : this.list) list.appendTag(new NBTTagString(uuid.toString()));
nbt.setTag(MOD_ID, list);
nbt.setBoolean(PUBLIC_KEY, pub);
}

public boolean isPublic()
{
return pub;
}

public void setPublic(boolean pub)
{
this.pub = pub;
}
}
Expand Up @@ -30,6 +30,7 @@
public class Constants
{
public static final String MOD_ID = "inventorylock";
public static final String PUBLIC_KEY = MOD_ID + "Public";
public static final String MOD_NAME = "InventoryLock";
public static final String ACTION = "Action";
public static final String UUIDS = "UUIDs";
Expand Down

0 comments on commit f35e93e

Please sign in to comment.