forked from lishid/OpenInv
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
792 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/main/java/com/lishid/openinv/internal/v1_9_R2/AnySilentChest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright (C) 2011-2014 lishid. All rights reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.lishid.openinv.internal.v1_9_R2; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.entity.Player; | ||
|
||
import com.lishid.openinv.internal.IAnySilentChest; | ||
|
||
// Volatile | ||
import net.minecraft.server.v1_9_R2.AxisAlignedBB; | ||
import net.minecraft.server.v1_9_R2.Block; | ||
import net.minecraft.server.v1_9_R2.BlockPosition; | ||
import net.minecraft.server.v1_9_R2.Entity; | ||
import net.minecraft.server.v1_9_R2.EntityOcelot; | ||
import net.minecraft.server.v1_9_R2.EntityPlayer; | ||
import net.minecraft.server.v1_9_R2.IInventory; | ||
import net.minecraft.server.v1_9_R2.ITileInventory; | ||
import net.minecraft.server.v1_9_R2.InventoryLargeChest; | ||
import net.minecraft.server.v1_9_R2.PacketPlayOutOpenWindow; | ||
import net.minecraft.server.v1_9_R2.TileEntityChest; | ||
import net.minecraft.server.v1_9_R2.World; | ||
|
||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer; | ||
|
||
public class AnySilentChest implements IAnySilentChest { | ||
@Override | ||
public boolean isAnyChestNeeded(Player p, int x, int y, int z) { | ||
// FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest | ||
EntityPlayer player = ((CraftPlayer) p).getHandle(); | ||
World world = player.world; | ||
// If block or ocelot on top | ||
if (world.getType(new BlockPosition(x, y + 1, z)).l() || hasOcelotOnTop(world, x, y, z)) | ||
return true; | ||
|
||
int id = Block.getId(world.getType(new BlockPosition(x, y, z)).getBlock()); | ||
|
||
// If block next to chest is chest and has a block or ocelot on top | ||
if (isBlockedChest(world, id, x - 1, y, z)) | ||
return true; | ||
if (isBlockedChest(world, id, x + 1, y, z)) | ||
return true; | ||
if (isBlockedChest(world, id, x, y, z - 1)) | ||
return true; | ||
if (isBlockedChest(world, id, x, y, z + 1)) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
private boolean isBlockedChest(World world, int id, int x, int y, int z) { | ||
BlockPosition position = new BlockPosition(x, y, z); | ||
if (Block.getId(world.getType(position).getBlock()) != id) { | ||
return false; | ||
} | ||
|
||
if (world.getType(position).l()) { | ||
return true; | ||
} | ||
|
||
return hasOcelotOnTop(world, x, y, z); | ||
} | ||
|
||
private boolean hasOcelotOnTop(World world, int x, int y, int z) { | ||
for (Entity localEntity : world.a(EntityOcelot.class, | ||
new AxisAlignedBB(x, y + 1, z, x + 1, y + 2, z + 1))) { | ||
EntityOcelot localEntityOcelot = (EntityOcelot) localEntity; | ||
if (localEntityOcelot.isSitting()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public boolean activateChest(Player p, boolean anychest, boolean silentchest, int x, int y, int z) { | ||
EntityPlayer player = ((CraftPlayer) p).getHandle(); | ||
World world = player.world; | ||
Object chest = world.getTileEntity(new BlockPosition(x, y, z)); | ||
if (chest == null) | ||
return true; | ||
|
||
int id = Block.getId(world.getType(new BlockPosition(x, y, z)).getBlock()); | ||
|
||
if (!anychest) { | ||
if (world.getType(new BlockPosition(x, y + 1, z)).l()) | ||
return true; | ||
if ((Block.getId(world.getType(new BlockPosition(x - 1, y, z)).getBlock()) == id) && (world.getType(new BlockPosition(x - 1, y + 1, z)).l())) | ||
return true; | ||
if ((Block.getId(world.getType(new BlockPosition(x + 1, y, z)).getBlock()) == id) && (world.getType(new BlockPosition(x + 1, y + 1, z)).l())) | ||
return true; | ||
if ((Block.getId(world.getType(new BlockPosition(x, y, z - 1)).getBlock()) == id) && (world.getType(new BlockPosition(x, y + 1, z - 1)).l())) | ||
return true; | ||
if ((Block.getId(world.getType(new BlockPosition(x, y, z + 1)).getBlock()) == id) && (world.getType(new BlockPosition(x, y + 1, z + 1)).l())) | ||
return true; | ||
} | ||
|
||
if (Block.getId(world.getType(new BlockPosition(x - 1, y, z)).getBlock()) == id) | ||
chest = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(new BlockPosition(x - 1, y, z)), (ITileInventory) chest); | ||
if (Block.getId(world.getType(new BlockPosition(x + 1, y, z)).getBlock()) == id) | ||
chest = new InventoryLargeChest("Large chest", (ITileInventory) chest, (TileEntityChest) world.getTileEntity(new BlockPosition(x + 1, y, z))); | ||
if (Block.getId(world.getType(new BlockPosition(x, y, z - 1)).getBlock()) == id) | ||
chest = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(new BlockPosition(x, y, z - 1)), (ITileInventory) chest); | ||
if (Block.getId(world.getType(new BlockPosition(x, y, z + 1)).getBlock()) == id) | ||
chest = new InventoryLargeChest("Large chest", (ITileInventory) chest, (TileEntityChest) world.getTileEntity(new BlockPosition(x, y, z + 1))); | ||
|
||
boolean returnValue = true; | ||
if (!silentchest) { | ||
player.openContainer((IInventory) chest); | ||
} | ||
else { | ||
try { | ||
SilentContainerChest silentContainerChest = new SilentContainerChest(player.inventory, ((IInventory) chest), player); | ||
int windowId = player.nextContainerCounter(); | ||
player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, "minecraft:chest", ((IInventory) chest).getScoreboardDisplayName(), ((IInventory) chest).getSize())); | ||
player.activeContainer = silentContainerChest; | ||
player.activeContainer.windowId = windowId; | ||
player.activeContainer.addSlotListener(player); | ||
returnValue = false; | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
p.sendMessage(ChatColor.RED + "Error while sending silent chest."); | ||
} | ||
} | ||
|
||
return returnValue; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/lishid/openinv/internal/v1_9_R2/InventoryAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (C) 2011-2014 lishid. All rights reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.lishid.openinv.internal.v1_9_R2; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import org.bukkit.entity.HumanEntity; | ||
import org.bukkit.inventory.Inventory; | ||
|
||
import com.lishid.openinv.OpenInv; | ||
import com.lishid.openinv.Permissions; | ||
import com.lishid.openinv.internal.IInventoryAccess; | ||
|
||
// Volatile | ||
import net.minecraft.server.v1_9_R2.IInventory; | ||
|
||
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftInventory; | ||
|
||
public class InventoryAccess implements IInventoryAccess { | ||
@Override | ||
public boolean check(Inventory inventory, HumanEntity player) { | ||
IInventory inv = grabInventory(inventory); | ||
|
||
if (inv instanceof SpecialPlayerInventory) { | ||
if (!OpenInv.hasPermission(player, Permissions.PERM_EDITINV)) { | ||
return false; | ||
} | ||
} | ||
|
||
else if (inv instanceof SpecialEnderChest) { | ||
if (!OpenInv.hasPermission(player, Permissions.PERM_EDITENDER)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private IInventory grabInventory(Inventory inventory) { | ||
if(inventory instanceof CraftInventory) { | ||
return ((CraftInventory) inventory).getInventory(); | ||
} | ||
|
||
//Use reflection to find the iinventory | ||
Class<? extends Inventory> clazz = inventory.getClass(); | ||
IInventory result = null; | ||
for(Field f : clazz.getDeclaredFields()) { | ||
f.setAccessible(true); | ||
if(IInventory.class.isAssignableFrom(f.getDeclaringClass())) { | ||
try { | ||
result = (IInventory) f.get(inventory); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/lishid/openinv/internal/v1_9_R2/PlayerDataManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (C) 2011-2014 lishid. All rights reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.lishid.openinv.internal.v1_9_R2; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.entity.Player; | ||
|
||
import com.mojang.authlib.GameProfile; | ||
|
||
// Volatile | ||
import net.minecraft.server.v1_9_R2.EntityPlayer; | ||
import net.minecraft.server.v1_9_R2.MinecraftServer; | ||
import net.minecraft.server.v1_9_R2.PlayerInteractManager; | ||
|
||
import org.bukkit.craftbukkit.v1_9_R2.CraftServer; | ||
|
||
public class PlayerDataManager extends com.lishid.openinv.internal.PlayerDataManager { | ||
|
||
@Override | ||
public Player loadOfflinePlayer(OfflinePlayer offline) { | ||
if (offline == null || !offline.hasPlayedBefore()) { | ||
return null; | ||
} | ||
GameProfile profile = new GameProfile(offline.getUniqueId(), offline.getName()); | ||
MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); | ||
// Create an entity to load the player data | ||
EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), profile, new PlayerInteractManager(server.getWorldServer(0))); | ||
|
||
// Get the bukkit entity | ||
Player target = (entity == null) ? null : entity.getBukkitEntity(); | ||
if (target != null) { | ||
// Load data | ||
target.loadData(); | ||
// Return the entity | ||
return target; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getPlayerDataID(OfflinePlayer player) { | ||
return player.getUniqueId().toString(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/lishid/openinv/internal/v1_9_R2/SilentContainerChest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2011-2014 lishid. All rights reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.lishid.openinv.internal.v1_9_R2; | ||
|
||
// Volatile | ||
import net.minecraft.server.v1_9_R2.ContainerChest; | ||
import net.minecraft.server.v1_9_R2.EntityHuman; | ||
import net.minecraft.server.v1_9_R2.IInventory; | ||
|
||
public class SilentContainerChest extends ContainerChest { | ||
public IInventory inv; | ||
|
||
public SilentContainerChest(IInventory i1, IInventory i2, EntityHuman e1) { | ||
super(i1, i2, e1); | ||
inv = i2; | ||
// close signal | ||
inv.closeContainer(e1); | ||
} | ||
|
||
@Override | ||
public void b(EntityHuman paramEntityHuman) { | ||
// Don't send close signal twice, might screw up | ||
} | ||
} |
Oops, something went wrong.
fc5f958
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
fc5f958
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I said in the release, it's currently untested but probably fine. I'll be testing it later tonight, but I don't have an environment set up yet.Tested, seems to work fine.