001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.bukkit.util; 020 021import com.google.common.collect.Maps; 022import com.plotsquared.core.location.World; 023import org.bukkit.Bukkit; 024import org.checkerframework.checker.nullness.qual.NonNull; 025 026import java.util.Map; 027 028public class BukkitWorld implements World<org.bukkit.World> { 029 030 private static final Map<String, BukkitWorld> worldMap = Maps.newHashMap(); 031 private static final boolean HAS_MIN_Y; 032 033 static { 034 boolean temp; 035 try { 036 org.bukkit.World.class.getMethod("getMinHeight"); 037 temp = true; 038 } catch (NoSuchMethodException e) { 039 temp = false; 040 } 041 HAS_MIN_Y = temp; 042 } 043 044 private final org.bukkit.World world; 045 046 private BukkitWorld(final org.bukkit.World world) { 047 this.world = world; 048 } 049 050 /** 051 * Get a new {@link BukkitWorld} from a world name 052 * 053 * @param worldName World name 054 * @return World instance 055 */ 056 public static @NonNull BukkitWorld of(final @NonNull String worldName) { 057 final org.bukkit.World bukkitWorld = Bukkit.getWorld(worldName); 058 if (bukkitWorld == null) { 059 throw new IllegalArgumentException(String.format("There is no world with the name '%s'", worldName)); 060 } 061 return of(bukkitWorld); 062 } 063 064 /** 065 * Get a new {@link BukkitWorld} from a Bukkit world 066 * 067 * @param world Bukkit world 068 * @return World instance 069 */ 070 public static @NonNull BukkitWorld of(final org.bukkit.World world) { 071 BukkitWorld bukkitWorld = worldMap.get(world.getName()); 072 if (bukkitWorld != null && bukkitWorld.getPlatformWorld().equals(world)) { 073 return bukkitWorld; 074 } 075 bukkitWorld = new BukkitWorld(world); 076 worldMap.put(world.getName(), bukkitWorld); 077 return bukkitWorld; 078 } 079 080 /** 081 * Get the min world height from a Bukkit {@link org.bukkit.World}. Inclusive 082 * 083 * @since 6.6.0 084 */ 085 public static int getMinWorldHeight(org.bukkit.World world) { 086 return HAS_MIN_Y ? world.getMinHeight() : 0; 087 } 088 089 /** 090 * Get the max world height from a Bukkit {@link org.bukkit.World}. Exclusive 091 * 092 * @since 6.6.0 093 */ 094 public static int getMaxWorldHeight(org.bukkit.World world) { 095 return HAS_MIN_Y ? world.getMaxHeight() : 256; 096 } 097 098 @Override 099 public org.bukkit.World getPlatformWorld() { 100 return this.world; 101 } 102 103 @Override 104 public @NonNull String getName() { 105 return this.world.getName(); 106 } 107 108 @Override 109 public int getMinHeight() { 110 return getMinWorldHeight(world); 111 } 112 113 @Override 114 public int getMaxHeight() { 115 return getMaxWorldHeight(world) - 1; 116 } 117 118 @Override 119 public boolean equals(final Object o) { 120 if (this == o) { 121 return true; 122 } 123 if (o == null || getClass() != o.getClass()) { 124 return false; 125 } 126 final BukkitWorld that = (BukkitWorld) o; 127 return world.equals(that.world); 128 } 129 130 @Override 131 public int hashCode() { 132 return world.hashCode(); 133 } 134 135 /** 136 * @deprecated This method is not meant to be invoked or overridden, with no replacement. 137 */ 138 @Deprecated(forRemoval = true, since = "6.6.0") 139 protected boolean canEqual(final Object other) { 140 return other instanceof BukkitWorld; 141 } 142 143 public String toString() { 144 return "BukkitWorld(world=" + this.world + ")"; 145 } 146 147}