001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk.event.block; 019 020import com.google.common.base.Preconditions; 021import net.tridentsdk.base.Block; 022import net.tridentsdk.docs.Policy; 023import net.tridentsdk.entity.living.Player; 024import net.tridentsdk.event.Cancellable; 025 026import javax.annotation.concurrent.GuardedBy; 027 028/** 029 * Called when a player edits a sign, or when the sign is first created 030 * 031 * @author The TridentSDK Team 032 * @since 0.3-alpha-DP 033 */ 034public class SignChangeEvent extends BlockEvent implements Cancellable { 035 private final Player editor; 036 @Policy(Policy.VOLATILE_ARRAY) 037 @GuardedBy("this") 038 private String[] contents; 039 private boolean cancelled; 040 041 public SignChangeEvent(Block block, Player editor, String... contents) { 042 super(block); 043 this.editor = editor; 044 this.contents = contents; 045 } 046 047 @Override 048 public boolean isIgnored() { 049 return cancelled; 050 } 051 052 @Override 053 public void cancel(boolean cancelled) { 054 this.cancelled = cancelled; 055 } 056 057 /** 058 * Returns the contents of the Sign 059 * 060 * @return String[] contents of the Sign 061 */ 062 public String[] contents() { 063 synchronized (this) { 064 return contents; 065 } 066 } 067 068 /** 069 * Sets the contents of the Sign 070 * 071 * @param contents String[] contents of the Sign 072 */ 073 public void setContents(String... contents) { 074 synchronized (this) { 075 this.contents = contents; 076 } 077 } 078 079 /** 080 * Returns the text of the specified line 081 * 082 * @param i line of the Sign 083 * @return String text of the specified line 084 */ 085 public String line(int i) { 086 Preconditions.checkArgument(i >= 0, "Sign line is below 0"); 087 Preconditions.checkNotNull(i <= 3, "Sign line is above 3"); 088 synchronized (this) { 089 return this.contents[i]; 090 } 091 } 092 093 /** 094 * Sets the value of a line 095 * 096 * @param i line of the Sign 097 * @param text String text to set the line as 098 * @return String previous text on the specified line 099 */ 100 public String setLine(int i, String text) { 101 Preconditions.checkNotNull(!text.isEmpty(), "Sign line length is below 0 characters"); 102 Preconditions.checkNotNull(text.length() <= 16, "Sign line length is above 16 characters"); 103 104 synchronized (this) { 105 String previous = this.contents[i]; 106 this.contents[i] = text; 107 return previous; 108 } 109 } 110 111 /** 112 * Returns the Player who edited the Sign 113 * 114 * @return Player editor of the sign, null if no player 115 */ 116 public Player editor() { 117 return this.editor; 118 } 119}