Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for MCPC+ and other general errors #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions MyPortals/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
disabled: false
debug: false

# time in ticks for the player to wait until they are teleported
# don't set this too low or they won't be able to get out of the portal before thery are teleported again
waitTime: 80

# locale prefix. you can also copy, rename and edit lang-EN.yml
locale: EN

Expand Down
3 changes: 2 additions & 1 deletion MyPortals/locale-EN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ hidden: can't display somebody's hidden portal info
locked: can't modify somebody's locked portal
invalidName: invalid name, only 1 to 12 ascii letters and numbers
busyName: the reciever has a same name portal
nameNotFound: portal name not found, try with "owner:portalName"
nameNotFound: "portal name not found, try with \"owner:portalName\""
diffrentWorlds: you can not link diffrent world portals for now
noName: give the portal a name first
notYours: you can not give a portal that is not yours
Expand All @@ -100,3 +100,4 @@ privacyOk: privacy successfully changed
giveOk: portal successfully transferred
recieveOk: player %$1s has gived you the portal %$2s
rebuilded: portals rebuilding finished
notEnoughXP: You need more XP to use this portal
4 changes: 2 additions & 2 deletions MyPortals/src/cl/netgamer/myportals/MyCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MyCmd implements CommandExecutor{

// METHODS

@Override
//@Override
public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args){

if(cmd.getName().equalsIgnoreCase("portal")){
Expand Down Expand Up @@ -304,7 +304,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String alias, String

// UTILITY

String msg(String key){
public String msg(String key){
if (key.length() < 20 && lang.msg.containsKey(key)) return lang.msg.get(key);
else return key;
}
Expand Down
83 changes: 70 additions & 13 deletions MyPortals/src/cl/netgamer/myportals/MyListener.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package cl.netgamer.myportals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerBucketEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
Expand Down Expand Up @@ -109,26 +112,43 @@ public void onPlayerInteract(PlayerInteractEvent event){
@EventHandler
public void onBlockPlace(BlockPlaceEvent event){
// check materials, from simple to complex, step by step
if(plugin.blockplacecooldown) return;
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
plugin.blockplacecooldown = false;
}
}, 2L);

//MyPortals.log("Placed Block: " + event.getBlock().getType().getId());

// check material of block placed, check only integer part is faster
if (event.getBlock().getTypeId() != ((Number) chargeId).intValue()){
return;
}
plugin.blockplacecooldown = true;

//MyPortals.log("Block is charge");

// check material of block below
if (event.getBlock().getLocation().clone().add(0, -1, 0).getBlock().getTypeId() != ((Number) baseId).intValue()){
//if (event.getBlock().getLocation().add(0, -1, 0).getBlock().getTypeId() != ((Number) plugin.portalId).intValue()){
return;
}

//MyPortals.log("Block is on base");

// possible portal, get location we will use a lot
Location loc = event.getBlock().getLocation().clone().add(0, -1, 0);
int facing = plugin.shape.getFacing(loc);
if (facing < 0) return;

//MyPortals.log("Facing = " + facing);

// looks like a portal, try to create
if (!plugin.create(event.getPlayer(), loc, facing)) return;

//MyPortals.log("Looks like a portal");

// add portal blocks, block -> base, base...
for (Location l: plugin.shape.getPortalBlocks(loc, facing)){
if (!portalBlocks.containsKey(l)) portalBlocks.put(l, new ArrayList<Portal>());
Expand Down Expand Up @@ -179,24 +199,34 @@ public void onBlockCanBuild(BlockCanBuildEvent event){
// on grab water portal with bucket
@EventHandler
public void onPlayerBucketFill(PlayerBucketFillEvent event){
checkPortalDestroyed(event.getBlockClicked());
if(isPartOfPortal(event.getBlockClicked())) {
event.setCancelled(true);
//checkPortalDestroyed(event.getBlockClicked());
}
}


// POSSIBLE PORTAL BLOCK DELETED

// on block event, check portal shape
private void checkPortalDestroyed(Block block){
// was portal material?
if (!(materials.contains(block.getTypeId()))){
return;
private boolean isPartOfPortal(Block block) {
// was portal material?
if (materials.contains(block.getTypeId())){
return true;
}

// belonged some portal?
if (!(portalBlocks.containsKey(block.getLocation()))){
return;
if (portalBlocks.containsKey(block.getLocation())){
return true;
}

return false;
}

// on block event, check portal shape
private void checkPortalDestroyed(Block block){

if(!isPartOfPortal(block)) return;

// what portals belonged to? (clone to avoid removing while iterating problems)
ArrayList<Portal> ofPortals = (ArrayList<Portal>)(portalBlocks.get(block.getLocation())).clone();

Expand All @@ -219,18 +249,45 @@ public void onPlayerMove(PlayerMoveEvent event){
@EventHandler
public void onStepBlock(StepBlockEvent event){
// player move?, cancel warp task if exists
//MyPortals.log("Step");
if (warps.containsKey(event.getPlayer().getName())){
warps.remove(event.getPlayer().getName());
event.getPlayer().removePotionEffect(PotionEffectType.getById(9));
event.getPlayer().removePotionEffect(PotionEffectType.CONFUSION);
}

// portal enter?: create warp task, schedule, store id and play nausea effect
if (plugin.getPortalByLocation(event.getTo()) == null) return;

WarpTask warp = new WarpTask(this, event.getPlayer(), event.getTo());
warp.runTaskLater(plugin, 80);
warp.runTaskLater(plugin, MyPortals.waitTime);
warps.put(event.getPlayer().getName(), warp.getTaskId());
event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getById(9), 160, 1));
event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 160, 1));
event.getPlayer().playEffect(event.getPlayer().getEyeLocation(), Effect.ENDER_SIGNAL, 10);
}

@EventHandler
public void onPistonExtend(BlockPistonExtendEvent event) {
for(Block block : event.getBlocks()) {
checkPortalDestroyed(block);
}
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Portal p = plugin.getPortalByLocation(event.getClickedBlock().getLocation());
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getItem().getType() == Material.COMPASS) {
if (p != null) {
if (p.canWarp(event.getPlayer())) return;
Location dest = p.getDestination();
if (dest != null) {
event.getPlayer().setCompassTarget(dest);
}
}
if (event.getPlayer().isSneaking()) {
Location spawn = event.getPlayer().getWorld().getSpawnLocation();
event.getPlayer().setCompassTarget(spawn);
}
}
}

// UTILITY
Expand Down
28 changes: 25 additions & 3 deletions MyPortals/src/cl/netgamer/myportals/MyPortals.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
Expand All @@ -24,6 +25,7 @@ public static void log(String msg){
logger.info(msg);
}

public static int waitTime;
// PROPERTIES

static boolean debug;
Expand All @@ -36,15 +38,27 @@ public static void log(String msg){
MyCmd cmd;
private String[] tags;

public boolean blockplacecooldown = false;

private static Lang lang;
public static String msg(String key){
if (key.length() < 20 && lang.msg.containsKey(key)) return lang.msg.get(key);
else return key;
}

// ENABLE PLUGIN

public void onEnable(){
this.saveDefaultConfig();

if (getConfig().getBoolean("disabled")){
getLogger().info("MyPortals disabled (does nothing)");
return;
}

lang = new Lang(this, getConfig().getString("locale"));

waitTime = getConfig().getInt("waitTime");
debug = getConfig().getBoolean("debug");
logger = getLogger();

Expand All @@ -60,6 +74,8 @@ public void onEnable(){
getCommand("portal").setExecutor(cmd);
tags = cmd.tags;
new MyListener(this, shape.getBaseId(), shape.getChargeId(), shape.getMaterials(), shape.getPortalsBlocks(portals));

logger.log(Level.INFO, Bukkit.getServer().getWorlds().toString());
}

// PORTAL INFORMATION
Expand Down Expand Up @@ -122,7 +138,13 @@ protected String info(Portal portal, String sender){
// PORTAL EVENTS

protected boolean create(Player owner, Location baseLoc, int facing){
if (!allowedWorlds.containsKey(baseLoc.getWorld().getName())) return false;
//MyPortals.log("Is this world (" + baseLoc.getWorld().getName() + ") Authorized?");
//MyPortals.log("Well, the list of autorized world is " + allowedWorlds.toString());
if (!allowedWorlds.containsKey(baseLoc.getWorld().getName())) {
//MyPortals.log("So, no.");
return false;
}
//MyPortals.log("So, yes.");
//Portal portal = new Portal(baseLoc, owner, facing);
Portal portal = new Portal(baseLoc, facing);
data.savePortal(portal);
Expand Down Expand Up @@ -168,7 +190,7 @@ protected String dest(Portal portal, String destName, Player player){

protected String setPrivacy(Portal portal, int privacy, Player player){
// try to change privacy
String status = setPrivacy(portal, privacy, player);
String status = portal.setPrivacy(privacy, player);
if (status.equals("privacyOk")) data.savePortal(portal);
return status;
}
Expand Down Expand Up @@ -262,7 +284,7 @@ static Location locationDecode(String data){
World w = null;
for (String s: allowedWorlds.keySet()){
if (allowedWorlds.get(s).equals(d[0])){
w = Bukkit.getWorld(s);
w = Bukkit.getServer().getWorld(s);
break;
}
}
Expand Down
6 changes: 5 additions & 1 deletion MyPortals/src/cl/netgamer/myportals/Portal.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ String setName(String portalName, Player player){
// can change name?
if (!player.getName().equalsIgnoreCase(owner) && privacy > 0) return "locked";
// check name syntax
if (!name.matches("[a-zA-Z0-9]{1,12}")) return "invalidName";
if (!portalName.matches("\\w{1,12}")) return "invalidName";
name = portalName;
// on first naming give ownership
if (owner.length() < 1) owner = player.getName();
Expand Down Expand Up @@ -133,6 +133,10 @@ String give(Player currentOwner, String recipient){
return "giveOk";
}

public boolean canWarp(Player player) {
return player.getName().equalsIgnoreCase(getOwner()) && privacy > 2;
}

boolean warp(Player player){
// allow teleport?
//if (!canWarp(player)) return false;
Expand Down
8 changes: 4 additions & 4 deletions MyPortals/src/cl/netgamer/myportals/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Map.Entry;
import org.bukkit.Location;
// 179
import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
//import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
// 1710
//import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;

Expand Down Expand Up @@ -226,10 +226,10 @@ class ShapeReplace implements ShapeInterface{
public boolean use(Location loc, Number ref, Location base){
// if replacing by red lamp on
if (ref.intValue() == 124){
boolean Static = ((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic;
((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = true;
//boolean Static = ((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic;
//((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = true;
loc.getBlock().setTypeId(124);
((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = Static;
//((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = Static;
return true;
}
loc.getBlock().setTypeIdAndData(ref.intValue(), (byte) ((ref.doubleValue() % 1) * 100), false);
Expand Down
18 changes: 11 additions & 7 deletions MyPortals/src/cl/netgamer/myportals/WarpTask.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cl.netgamer.myportals;

import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

public class WarpTask extends BukkitRunnable{
public class WarpTask extends BukkitRunnable {

private MyListener parent;
private Player player;
Expand All @@ -18,17 +19,13 @@ public WarpTask(MyListener parent, Player player, Location location){
}

// this runs every tick?
@Override
//@Override
public void run(){
// canceled?
if (!parent.warps.containsValue(this.getTaskId())) return;

// remove nausea effect before
player.removePotionEffect(PotionEffectType.getById(9));

// player has enough experience points?
int xp = player.getTotalExperience();
if (xp < parent.plugin.xpCost) return;
player.removePotionEffect(PotionEffectType.CONFUSION);

// source portal still exists?
Portal portal = parent.plugin.getPortalByLocation(location);
Expand All @@ -38,6 +35,13 @@ public void run(){
portal = parent.plugin.getPortalByLocation(portal.getDestination());
if (portal == null) return;

// player has enough experience points?
int xp = player.getTotalExperience();
if (xp < parent.plugin.xpCost) {
player.playSound(player.getLocation(), Sound.VILLAGER_NO, 5, 0);
return;
}

// allow teleport?
//if (!portal.canWarp(player)) return;

Expand Down
Loading