Permalink
Browse files

Start the tile entity refactor

  • Loading branch information...
me4502 committed Oct 3, 2017
1 parent 6f8e5ba commit 2d301eeae002542435b9d10b44c8da4652a33e00
@@ -11,19 +11,21 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import java.util.Arrays;
import java.util.Locale;
public class ChangedSign {
private Block block;
private Sign sign;
private String[] lines;
public ChangedSign(Sign sign, String[] lines, LocalPlayer player) {
public ChangedSign(Block block, String[] lines, LocalPlayer player) {
this(sign, lines);
this(block, lines);
if(lines != null && VariableManager.instance != null) {
for(int i = 0; i < 4; i++) {
@@ -46,19 +48,24 @@ public ChangedSign(Sign sign, String[] lines, LocalPlayer player) {
}
}
public ChangedSign(Sign sign, String[] lines) {
public ChangedSign(Block block, String[] lines) {
Validate.notNull(sign);
this.sign = sign;
this.block = block;
this.lines = lines;
this.sign = (Sign) block.getState();
}
public BlockWorldVector getBlockVector() {
return BukkitUtil.toWorldVector(sign.getBlock());
}
public Block getBlock() {
return block;
}
public Sign getSign() {
return sign;
@@ -10,7 +10,36 @@
import com.sk89q.craftbook.core.LanguageManager;
import com.sk89q.craftbook.core.st.MechanicClock;
import com.sk89q.craftbook.core.st.SelfTriggeringManager;
import com.sk89q.craftbook.mechanics.*;
import com.sk89q.craftbook.mechanics.AIMechanic;
import com.sk89q.craftbook.mechanics.Ammeter;
import com.sk89q.craftbook.mechanics.BetterLeads;
import com.sk89q.craftbook.mechanics.BetterPhysics;
import com.sk89q.craftbook.mechanics.BetterPistons;
import com.sk89q.craftbook.mechanics.BetterPlants;
import com.sk89q.craftbook.mechanics.Bookcase;
import com.sk89q.craftbook.mechanics.BounceBlocks;
import com.sk89q.craftbook.mechanics.Chair;
import com.sk89q.craftbook.mechanics.ChunkAnchor;
import com.sk89q.craftbook.mechanics.CommandSigns;
import com.sk89q.craftbook.mechanics.CookingPot;
import com.sk89q.craftbook.mechanics.Elevator;
import com.sk89q.craftbook.mechanics.Footprints;
import com.sk89q.craftbook.mechanics.GlowStone;
import com.sk89q.craftbook.mechanics.HiddenSwitch;
import com.sk89q.craftbook.mechanics.JackOLantern;
import com.sk89q.craftbook.mechanics.LightStone;
import com.sk89q.craftbook.mechanics.LightSwitch;
import com.sk89q.craftbook.mechanics.MapChanger;
import com.sk89q.craftbook.mechanics.Marquee;
import com.sk89q.craftbook.mechanics.Netherrack;
import com.sk89q.craftbook.mechanics.PaintingSwitch;
import com.sk89q.craftbook.mechanics.Payment;
import com.sk89q.craftbook.mechanics.RedstoneJukebox;
import com.sk89q.craftbook.mechanics.Snow;
import com.sk89q.craftbook.mechanics.Sponge;
import com.sk89q.craftbook.mechanics.Teleporter;
import com.sk89q.craftbook.mechanics.TreeLopper;
import com.sk89q.craftbook.mechanics.XPStorer;
import com.sk89q.craftbook.mechanics.area.Area;
import com.sk89q.craftbook.mechanics.area.simple.Bridge;
import com.sk89q.craftbook.mechanics.area.simple.Door;
@@ -29,19 +58,57 @@
import com.sk89q.craftbook.mechanics.ic.ICMechanic;
import com.sk89q.craftbook.mechanics.items.CommandItemDefinition;
import com.sk89q.craftbook.mechanics.items.CommandItems;
import com.sk89q.craftbook.mechanics.minecart.*;
import com.sk89q.craftbook.mechanics.minecart.blocks.*;
import com.sk89q.craftbook.mechanics.minecart.CollisionEntry;
import com.sk89q.craftbook.mechanics.minecart.ConstantSpeed;
import com.sk89q.craftbook.mechanics.minecart.EmptyDecay;
import com.sk89q.craftbook.mechanics.minecart.EmptySlowdown;
import com.sk89q.craftbook.mechanics.minecart.FallModifier;
import com.sk89q.craftbook.mechanics.minecart.ItemPickup;
import com.sk89q.craftbook.mechanics.minecart.MobBlocker;
import com.sk89q.craftbook.mechanics.minecart.MoreRails;
import com.sk89q.craftbook.mechanics.minecart.NoCollide;
import com.sk89q.craftbook.mechanics.minecart.PlaceAnywhere;
import com.sk89q.craftbook.mechanics.minecart.RailPlacer;
import com.sk89q.craftbook.mechanics.minecart.TemporaryCart;
import com.sk89q.craftbook.mechanics.minecart.VisionSteering;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartBlockMechanism;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartBooster;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartDeposit;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartDispenser;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartEjector;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartLift;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartMaxSpeed;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartMessenger;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartReverser;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartSorter;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartStation;
import com.sk89q.craftbook.mechanics.minecart.blocks.CartTeleporter;
import com.sk89q.craftbook.mechanics.pipe.Pipes;
import com.sk89q.craftbook.mechanics.signcopier.SignCopier;
import com.sk89q.craftbook.mechanics.variables.VariableManager;
import com.sk89q.craftbook.util.*;
import com.sk89q.craftbook.util.ArrayUtil;
import com.sk89q.craftbook.util.CompatabilityUtil;
import com.sk89q.craftbook.util.ItemSyntax;
import com.sk89q.craftbook.util.RegexUtil;
import com.sk89q.craftbook.util.UUIDMappings;
import com.sk89q.craftbook.util.compat.companion.CompanionPlugins;
import com.sk89q.craftbook.util.compat.nms.NMSAdapter;
import com.sk89q.craftbook.util.persistent.PersistentStorage;
import com.sk89q.minecraft.util.commands.*;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
import com.sk89q.minecraft.util.commands.CommandUsageException;
import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.minecraft.util.commands.MissingNestedCommandException;
import com.sk89q.minecraft.util.commands.SimpleInjector;
import com.sk89q.minecraft.util.commands.WrappedCommandException;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.wepif.PermissionsResolverManager;
import org.bukkit.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
@@ -53,16 +120,31 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.Nullable;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import javax.annotation.Nullable;
public class CraftBookPlugin extends JavaPlugin {
/**
@@ -127,6 +209,11 @@
*/
private SelfTriggeringManager selfTriggerManager;
/**
* The NMS Adapter.
*/
private NMSAdapter nmsAdapter;
public static final Map<String, Class<? extends CraftBookMechanic>> availableMechanics;
public boolean useLegacyCartSystem = false;
@@ -269,6 +356,19 @@ public UUIDMappings getUUIDMappings() {
return uuidMappings;
}
/**
* Retrieve the NMS Adapter.
*
* <p>
* Note: This may not actually be using NMS.
* </p>
*
* @return The NMS Adapter
*/
public NMSAdapter getNmsAdapter() {
return this.nmsAdapter;
}
/**
* Called on plugin enable.
*/
@@ -305,6 +405,8 @@ public void onEnable() {
uuidMappings = new UUIDMappings();
uuidMappings.enable();
nmsAdapter = new NMSAdapter();
logDebugMessage("Initializing Managers!", "startup");
managerAdapter = new MechanicListenerAdapter();
@@ -40,33 +40,24 @@
public final class BukkitUtil {
public static void printStacktrace(Throwable e) {
CraftBookPlugin.inst().getLogger().severe(CraftBookPlugin.getStackTrace(e));
}
public static ChangedSign toChangedSign(Sign sign) {
return toChangedSign(sign, sign.getLines());
}
public static ChangedSign toChangedSign(Block sign) {
if (!SignUtil.isSign(sign)) return null;
return toChangedSign((Sign) sign.getState(), ((Sign) sign.getState()).getLines());
return toChangedSign(sign, ((Sign) sign.getState()).getLines());
}
public static ChangedSign toChangedSign(Sign sign, String[] lines) {
return new ChangedSign(sign, lines);
public static ChangedSign toChangedSign(Block block, String[] lines) {
return toChangedSign(block, lines, null);
}
public static ChangedSign toChangedSign(Sign sign, String[] lines, LocalPlayer player) {
return new ChangedSign(sign, lines, player);
public static ChangedSign toChangedSign(Block block, String[] lines, LocalPlayer player) {
if (!SignUtil.isSign(block)) return null;
return new ChangedSign(block, lines, player);
}
public static Block toBlock(ChangedSign sign) {
return sign.getSign().getBlock();
return sign.getBlock();
}
public static Sign toSign(ChangedSign sign) {
@@ -16,22 +16,6 @@
package com.sk89q.craftbook.mechanics.ic;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import com.sk89q.craftbook.AbstractCraftBookMechanic;
import com.sk89q.craftbook.ChangedSign;
import com.sk89q.craftbook.LocalPlayer;
@@ -50,6 +34,20 @@
import com.sk89q.craftbook.util.events.SignClickEvent;
import com.sk89q.craftbook.util.events.SourcedBlockRedstoneEvent;
import com.sk89q.util.yaml.YAMLProcessor;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
/**
* Mechanic wrapper for ICs. The mechanic manager dispatches events to this mechanic,
@@ -436,7 +434,7 @@ else if(event.getLine(1).equalsIgnoreCase("[mc0421]"))
Bukkit.getServer().getScheduler().runTask(CraftBookPlugin.inst(), () -> {
ChangedSign sign = new ChangedSign((Sign) event.getBlock().getState(), event.getLines());
ChangedSign sign = new ChangedSign(event.getBlock(), event.getLines());
//WorldEdit offset/radius tools.
ICUtil.parseSignFlags(player, sign);
@@ -13,7 +13,6 @@
import com.sk89q.craftbook.util.exceptions.InsufficientPermissionsException;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.event.EventHandler;
@@ -158,7 +157,7 @@ public void onSignChange(SignChangeEvent event) {
}
}
if (!found) return;
if (!verify(BukkitUtil.toChangedSign((Sign) event.getBlock().getState(), lines, player), player)) {
if (!verify(BukkitUtil.toChangedSign(event.getBlock(), lines, player), player)) {
block.breakNaturally();
event.setCancelled(true);
return;
@@ -0,0 +1,15 @@
package com.sk89q.craftbook.util.compat.nms;
public class NMSAdapter {
/**
* Gets whether an NMS extension is installed.
*
* @return If an NMS extension is installed
*/
public boolean hasNMSExtension() {
return false;
}
}
@@ -2,15 +2,16 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.sk89q.craftbook.ChangedSign;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.sk89q.craftbook.ChangedSign;
@RunWith(PowerMockRunner.class)
@PrepareForTest(ChangedSign.class)
public class BukkitChangedSignTest {
@@ -20,7 +21,10 @@ public void testBukkitChangedSign() {
new ChangedSign(null, null);
ChangedSign sign = new ChangedSign(mock(Sign.class), new String[]{"","","",""});
Block mockBlock = mock(Block.class);
when(mockBlock.getState()).thenReturn(mock(Sign.class));
ChangedSign sign = new ChangedSign(mockBlock, new String[]{"","","",""});
assertTrue(sign.getSign() != null);
assertTrue(sign.getLines().length == 4);
}

0 comments on commit 2d301ee

Please sign in to comment.