Skip to content

Commit

Permalink
Redone compass functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Baumann committed Jan 14, 2014
1 parent 1d3f395 commit 5a7dd43
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 159 deletions.
45 changes: 14 additions & 31 deletions src/com/dre/managerxl/listeners/PlayerListener.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.dre.managerxl.listeners;

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.AsyncPlayerChatEvent;
Expand Down Expand Up @@ -105,36 +102,22 @@ public void onPlayerInteract(PlayerInteractEvent event) {
if (P.p.getPermissionHandler().has(player, "mxl.tool.tpcompass")) {
if (event.hasItem()) {
if (event.getItem().getType() == Material.COMPASS) {
Location seeLocation = null;
List<Block> sight = player.getLastTwoTargetBlocks(null, 100);
boolean hitStone = false;

for (Block block : sight) {
if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
if (block.getTypeId() != 0) {
hitStone = true;
}
if (block.getTypeId() == 0 && hitStone) {
seeLocation = block.getLocation();
break;
Block lastBlockInSight = MUtility.getLastBlockInSight(player, 100);

if(lastBlockInSight != null){
if(lastBlockInSight.getType() != Material.AIR){
Location seeLocation = lastBlockInSight.getLocation();
if (seeLocation != null) {
seeLocation.setPitch(player.getLocation().getPitch());
seeLocation.setYaw(player.getLocation().getYaw());

if (player.getLocation().getY() > seeLocation.getY()) {
seeLocation.setY(seeLocation.getY() + 1);
}

player.teleport(seeLocation);
}
} else {
if (block.getTypeId() != 0) {
seeLocation = block.getLocation();
break;
}
}
}

if (seeLocation != null) {
seeLocation.setPitch(player.getLocation().getPitch());
seeLocation.setYaw(player.getLocation().getYaw());

if (player.getLocation().getY() > seeLocation.getY()) {
seeLocation.setY(seeLocation.getY() + 1);
}

player.teleport(seeLocation);
}

event.setCancelled(true);
Expand Down
277 changes: 149 additions & 128 deletions src/com/dre/managerxl/util/MUtility.java
Original file line number Diff line number Diff line change
@@ -1,128 +1,149 @@
package com.dre.managerxl.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;

import com.dre.managerxl.P;

public class MUtility {
public static String parseMessage(String[] args, int start) {
String message = "";

for (int i = start; i < args.length; i++) {
message += args[i] + " ";
}

message = message.trim();

return message;
}

public static int parseInt(String string) {
return NumberUtils.toInt(string, 0);
}

public static int getStringTimeToInt(String time) {
int result = 0;
int index = 0;
int lastIndex = 0;

// Days
index = time.indexOf("d");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex, index)) * 24 * 60 * 60;
}

// Hours
lastIndex = index;
index = time.indexOf("h");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex + 1, index)) * 60 * 60;
}

// Minutes
lastIndex = index;
index = time.indexOf("m");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex + 1, index)) * 60;
}

// Seconds
lastIndex = index;
index = time.indexOf("s");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex + 1, index));
}

return result * 1000; // Seconds to millisecs
}

public static String getIntTimeToString(long l) {
String result = "";

l = l / 1000; // Millisecs to seconds

// Days
int dayTime = 24 * 60 * 60;
if (l >= dayTime) {
result += ((int) (l / dayTime)) + " " + P.p.getLanguageReader().get("Format_Days") + " ";
l = l % dayTime;
}

// Hours
int hourTime = 60 * 60;
if (l >= hourTime) {
result += ((int) (l / hourTime)) + " " + P.p.getLanguageReader().get("Format_Hours") + " ";
l = l % hourTime;
}

// Minute
int minuteTime = 60;
if (l >= minuteTime) {
result += ((int) (l / minuteTime)) + " " + P.p.getLanguageReader().get("Format_Minutes") + " ";
l = l % minuteTime;
}

// Seconds
if (l >= 1) {
result += l + " " + P.p.getLanguageReader().get("Format_Seconds") + " ";
l = l % minuteTime;
}

return result.trim();
}

public static Location getNearestFreePosition(Location currentPosition) {
Block block = currentPosition.getBlock();

for (int y = 0; y < 256 - block.getY(); y++) {
Block tmpBlock = block.getRelative(BlockFace.UP, y);

if (tmpBlock.getTypeId() == 0) {
if(tmpBlock.getRelative(BlockFace.UP, 1).getTypeId() == 0){
return tmpBlock.getLocation();
}
}
}

return new Location(block.getWorld(), block.getX(), 256, block.getZ());
}

public static long getStringDateToLong(String date, String time) {
SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date d = null;
try {
d = f.parse(date + " " + time);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return d.getTime();
}
}
package com.dre.managerxl.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.LivingEntity;
import org.bukkit.util.BlockIterator;

import com.dre.managerxl.P;

public class MUtility {
public static String parseMessage(String[] args, int start) {
String message = "";

for (int i = start; i < args.length; i++) {
message += args[i] + " ";
}

message = message.trim();

return message;
}

public static int parseInt(String string) {
return NumberUtils.toInt(string, 0);
}

public static int getStringTimeToInt(String time) {
int result = 0;
int index = 0;
int lastIndex = 0;

// Days
index = time.indexOf("d");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex, index)) * 24 * 60 * 60;
}

// Hours
lastIndex = index;
index = time.indexOf("h");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex + 1, index)) * 60 * 60;
}

// Minutes
lastIndex = index;
index = time.indexOf("m");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex + 1, index)) * 60;
}

// Seconds
lastIndex = index;
index = time.indexOf("s");
if (index > lastIndex) {
result += parseInt(time.substring(lastIndex + 1, index));
}

return result * 1000; // Seconds to millisecs
}

public static String getIntTimeToString(long l) {
String result = "";

l = l / 1000; // Millisecs to seconds

// Days
int dayTime = 24 * 60 * 60;
if (l >= dayTime) {
result += ((int) (l / dayTime)) + " " + P.p.getLanguageReader().get("Format_Days") + " ";
l = l % dayTime;
}

// Hours
int hourTime = 60 * 60;
if (l >= hourTime) {
result += ((int) (l / hourTime)) + " " + P.p.getLanguageReader().get("Format_Hours") + " ";
l = l % hourTime;
}

// Minute
int minuteTime = 60;
if (l >= minuteTime) {
result += ((int) (l / minuteTime)) + " " + P.p.getLanguageReader().get("Format_Minutes") + " ";
l = l % minuteTime;
}

// Seconds
if (l >= 1) {
result += l + " " + P.p.getLanguageReader().get("Format_Seconds") + " ";
l = l % minuteTime;
}

return result.trim();
}

public static Location getNearestFreePosition(Location currentPosition) {
Block block = currentPosition.getBlock();

for (int y = 0; y < 256 - block.getY(); y++) {
Block tmpBlock = block.getRelative(BlockFace.UP, y);

if (tmpBlock.getTypeId() == 0) {
if(tmpBlock.getRelative(BlockFace.UP, 1).getTypeId() == 0){
return tmpBlock.getLocation();
}
}
}

return new Location(block.getWorld(), block.getX(), 256, block.getZ());
}

public static long getStringDateToLong(String date, String time) {
SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date d = null;
try {
d = f.parse(date + " " + time);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return d.getTime();
}

public static Block getLastBlockInSight(LivingEntity entity, int distance){
Block block = null;

if (entity != null) {
BlockIterator bit = new BlockIterator(entity, distance);

while(bit.hasNext())
{
block = bit.next();
if (block.getType() != Material.AIR){
break;
}
}
}

return block;
}
}

0 comments on commit 5a7dd43

Please sign in to comment.