Added Noteblock change and play events. Closes #1027 #1025

This commit is contained in:
Lex Manos committed 2014-06-02 16:57:04 -07:00
1 parent b301b1f70c
commit 42fc3c9516
4 files changed
+216

No files matched your search

@@ -3,6 +3,7 @@ package net.minecraftforge.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.block.Block;
@@ -25,6 +26,7 @@ import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S23PacketBlockChange;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityNote;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.DamageSource;
@@ -47,6 +49,7 @@ import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.event.entity.player.PlayerOpenContainerEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.NoteBlockEvent;
import static net.minecraft.init.Blocks.*;
public class ForgeHooks
@@ -436,4 +439,16 @@ public class ForgeHooks
container.maximumCost = e.cost;
return false;
}
public static boolean onNoteChange(TileEntityNote te, byte old)
{
NoteBlockEvent.Change e = new NoteBlockEvent.Change(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord, te.getBlockMetadata(), old, te.note);
if (MinecraftForge.EVENT_BUS.post(e))
{
te.note = old;
return false;
}
te.note = (byte)e.getVanillaNoteId();
return true;
}
}
@@ -0,0 +1,165 @@
package net.minecraftforge.event.world;
import com.google.common.base.Preconditions;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent;
import cpw.mods.fml.common.eventhandler.Cancelable;
import cpw.mods.fml.common.eventhandler.Event;
/**
* Base class for Noteblock Events
*
*/
public class NoteBlockEvent extends BlockEvent
{
private int noteId;
NoteBlockEvent(World world, int x, int y, int z, int meta, int note)
{
super(x, y, z, world, Blocks.noteblock, meta);
this.noteId = note;
}
/**
* Get the Note the Noteblock is tuned to
* @return the Note
*/
public Note getNote()
{
return Note.fromId(noteId);
}
/**
* Get the Octave of the note this Noteblock is tuned to
* @return the Octave
*/
public Octave getOctave()
{
return Octave.fromId(noteId);
}
/**
* get the vanilla note-id, which contains information about both Note and Octave. Most modders should not need this.
* @return an ID for the note
*/
public int getVanillaNoteId()
{
return noteId;
}
/**
* Set Note and Octave for this event.<br>
* If octave is Octave.HIGH, note may only be Note.F_SHARP
* @param note the Note
* @param octave the Octave
*/
public void setNote(Note note, Octave octave)
{
Preconditions.checkArgument(octave != Octave.HIGH || note == Note.F_SHARP, "Octave.HIGH is only valid for Note.F_SHARP!");
this.noteId = note.ordinal() + octave.ordinal() * 12;
}
/**
* Fired when a Noteblock plays it's note. You can override the note and instrument
* Canceling this event will stop the note from playing.
*/
@Cancelable
public static class Play extends NoteBlockEvent
{
public Instrument instrument;
public Play(World world, int x, int y, int z, int meta, int note, int instrument)
{
super(world, x, y, z, meta, note);
this.instrument = Instrument.fromId(instrument);
}
}
/**
* Fired when a Noteblock is changed. You can adjust the note it will change to via {@link #setNote(Note, Octave)}.
* Canceling this event will not change the note and also stop the Noteblock from playing it's note.
*/
@Cancelable
public static class Change extends NoteBlockEvent
{
public final Note oldNote;
public final Octave oldOctave;
public Change(World world, int x, int y, int z, int meta, int oldNote, int newNote)
{
super(world, x, y, z, meta, newNote);
this.oldNote = Note.fromId(oldNote);
this.oldOctave = Octave.fromId(oldNote);
}
}
/**
* Describes the types of musical Instruments that can be played by a Noteblock.
* The Instrument being played can be overridden with {@link NoteBlockEvent.Play#setInstrument(Instrument)}
*/
public static enum Instrument
{
PIANO,
BASSDRUM,
SNARE,
CLICKS,
BASSGUITAR;
// cache to avoid creating a new array every time
private static final Instrument[] values = values();
static Instrument fromId(int id)
{
return id < 0 || id > 4 ? PIANO : values[id];
}
}
/**
* Information about the pitch of a Noteblock note.
* For altered notes such as G-Sharp / A-Flat the Sharp variant is used here.
*
*/
public static enum Note
{
F_SHARP,
G,
G_SHARP,
A,
A_SHARP,
B,
C,
C_SHARP,
D,
D_SHARP,
E,
F;
private static final Note[] values = values();
static Note fromId(int id)
{
return values[id % 12];
}
}
/**
* Describes the Octave of a Note being played by a Noteblock.
* Together with {@link Note} it fully describes the note.
*
*/
public static enum Octave
{
LOW,
MID,
HIGH; // only valid for F_SHARP
static Octave fromId(int id)
{
return id < 12 ? LOW : id == 24 ? HIGH : MID;
}
}
}