Skip to content

Commit

Permalink
v0.4, Update to SpongeAPI 5, remove NMS dependencies now api supports.
Browse files Browse the repository at this point in the history
Clicking the radio again will stop the current song
  • Loading branch information
clienthax committed Sep 22, 2016
1 parent 51ca35d commit c2ecb44
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build

## Directory-based project format:
.idea/
run
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT'
classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT'
}
}

Expand All @@ -29,6 +29,7 @@ ext {
minecraft {
forgeVersion = project.forgeVersion
mappings = project.mcpMappings
runDir = "run"
}

repositories {
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name=MusicBox
group=uk.co.haxyshideout
url=http://github.com/clienthax
organization=haxyshideout
version=0.3
apiVersion=4.1.0-SNAPSHOT
forgeVersion=1.8.9-11.15.1.1765
mcpMappings=snapshot_20160301
version=0.4
apiVersion=5.0.0-SNAPSHOT
forgeVersion=1.10.2-12.18.1.2088
mcpMappings=snapshot_20160915
2 changes: 1 addition & 1 deletion src/main/java/uk/co/haxyshideout/musicbox/MusicBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import java.io.File;

@Plugin(name = "MusicBox", id = "uk.co.haxyshideout.musicbox", dependencies = @Dependency(id = "com.xxmicloxx.noteblockapi"))
@Plugin(name = "MusicBox", id = "musicbox", dependencies = @Dependency(id = "noteblockapi"))
public class MusicBox {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ public MusicBoxSettingsData asMutable() {
return new MusicBoxSettingsData(this.musicBoxType, this.inventoryDirection, this.inventorySlot);
}

@Override
public int compareTo(ImmutableMusicBoxSettingsData o) {
ComparisonChain comparisonChain = ComparisonChain.start()
.compare(this.musicBoxType, o.musicBoxType);
if(this.inventoryDirection != null && o.inventoryDirection != null) {
comparisonChain = comparisonChain.compare(this.inventoryDirection, o.inventoryDirection);
}
if(this.inventorySlot != null && o.inventorySlot != null) {
comparisonChain = comparisonChain.compare(this.inventorySlot, o.inventorySlot);
}
return comparisonChain.result();
}

@Override
public int getContentVersion() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.co.haxyshideout.musicbox.data.spongedata;

import com.google.common.reflect.TypeToken;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.key.KeyFactory;
import org.spongepowered.api.data.value.mutable.OptionalValue;
Expand All @@ -8,15 +9,26 @@

import static org.spongepowered.api.data.DataQuery.of;

import java.util.Optional;

public class MusicBoxKeys {

public enum MusicBoxType {
STANDARD,
CHEST
}

public static final Key<Value<MusicBoxType>> MUSIC_BOX_TYPE = KeyFactory.makeSingleKey(MusicBoxKeys.MusicBoxType.class, Value.class, of("musicBoxType"));
public static final Key<OptionalValue<Direction>> INVENTORY_DIRECTION = KeyFactory.makeOptionalKey(Direction.class, of("invDirection"));
public static final Key<OptionalValue<Integer>> INVENTORY_SLOT = KeyFactory.makeOptionalKey(Integer.class, of("invSlot"));
private static final TypeToken<MusicBoxKeys.MusicBoxType> MUSIC_BOX_TYPE_TOKEN = new TypeToken<MusicBoxType>() {};
private static final TypeToken<Value<MusicBoxKeys.MusicBoxType>> VALUE_MUSIC_BOX_TYPE = new TypeToken<Value<MusicBoxType>>() {};
public static final Key<Value<MusicBoxType>> MUSIC_BOX_TYPE = KeyFactory.makeSingleKey(MUSIC_BOX_TYPE_TOKEN, VALUE_MUSIC_BOX_TYPE, of("musicBoxType"), "musicbox:musicboxtype", "Music Box Type");

private static final TypeToken<Optional<Direction>> DIRECTION_TYPE_TOKEN = new TypeToken<Optional<Direction>>() {};
private static final TypeToken<OptionalValue<Direction>> VALUE_DIRECTION = new TypeToken<OptionalValue<Direction>>() {};
public static final Key<OptionalValue<Direction>> INVENTORY_DIRECTION = KeyFactory.makeOptionalKey(DIRECTION_TYPE_TOKEN, VALUE_DIRECTION, of("invDirection"), "musicbox:invdirection", "Music Box Inv Direction");

private static final TypeToken<Optional<Integer>> INTEGER_TYPE_TOKEN = new TypeToken<Optional<Integer>>() {};
private static final TypeToken<OptionalValue<Integer>> VALUE_INTEGER = new TypeToken<OptionalValue<Integer>>() {};
public static final Key<OptionalValue<Integer>> INVENTORY_SLOT = KeyFactory.makeOptionalKey(INTEGER_TYPE_TOKEN, VALUE_INTEGER, of("invSlot"), "musicbox:invslot", "Music Box Inv Slot");


}
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,6 @@ public ImmutableMusicBoxSettingsData asImmutable() {
return new ImmutableMusicBoxSettingsData(this.musicBoxType, this.inventoryDirection, this.inventorySlot);
}

@Override
public int compareTo(MusicBoxSettingsData o) {
ComparisonChain comparisonChain = ComparisonChain.start()
.compare(this.musicBoxType, o.musicBoxType);
if(this.inventoryDirection != null && o.inventoryDirection != null) {
comparisonChain = comparisonChain.compare(this.inventoryDirection, o.inventoryDirection);
}
if(this.inventorySlot != null && o.inventorySlot != null) {
comparisonChain = comparisonChain.compare(this.inventorySlot, o.inventorySlot);
}
return comparisonChain.result();
}

@Override
public int getContentVersion() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import com.xxmicloxx.NoteBlockAPI.decoders.nbs.Song;
import com.xxmicloxx.NoteBlockAPI.events.SongEndEvent;
import com.xxmicloxx.NoteBlockAPI.players.NoteBlockSongPlayer;
import net.minecraft.inventory.IInventory;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.tileentity.Jukebox;
import org.spongepowered.api.block.tileentity.TileEntity;
import org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier;
import org.spongepowered.api.data.Transaction;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
Expand All @@ -22,8 +21,9 @@
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.property.SlotIndex;
import org.spongepowered.api.item.inventory.type.TileEntityInventory;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
import org.spongepowered.api.text.format.TextColors;
Expand All @@ -37,7 +37,6 @@
import uk.co.haxyshideout.musicbox.data.spongedata.MusicBoxSettingsData;

import java.util.Collection;
import java.util.HashMap;
import java.util.Optional;

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -82,20 +81,25 @@ public void onJukeboxBroke(ChangeBlockEvent.Break event) {

@Listener
public void onItemInteract(InteractBlockEvent.Secondary event, @First Player player) {
Optional<ItemStack> itemInHand = player.getItemInHand();
Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
//noinspection ConstantConditions
if (itemInHand.isPresent() && itemInHand.get().getItem() == ItemTypes
.JUKEBOX && itemInHand.get().get(Keys.DISPLAY_NAME).isPresent()) {
String itemName = TextSerializers.PLAIN.serialize(itemInHand.get().get(Keys.DISPLAY_NAME).get());
if (itemName.equals("Radio")) {
event.setCancelled(true);
//Stop the radio from playing
if(PlaySongCommand.radioSongPlayers.containsKey(player.getUniqueId())) {
PlaySongCommand.radioSongPlayers.get(player.getUniqueId()).setPlaying(false);
}
//Send the song list to the player
MusicBox.getInstance().getSongStore().sendPlaySongList(player);
}
}
}

@Listener
public void onInteractWithJukebox(InteractBlockEvent.Secondary event, @First Player player) {
public void onInteractWithJukebox(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
//If the block interacted with is not a jukebox, return
//noinspection ConstantConditions
if (event.getTargetBlock().getState().getType() != BlockTypes.JUKEBOX) {
Expand Down Expand Up @@ -124,15 +128,11 @@ public void onInteractWithJukebox(InteractBlockEvent.Secondary event, @First Pla
for (Direction direction : CARDINAL_SET) {
Optional<TileEntity> teNextToJukeboxOptional = worldLocation.add(direction.toVector3d()).getTileEntity();
if(teNextToJukeboxOptional.isPresent()) {

//TODO One day. i will be able to do this with the api -.-
net.minecraft.tileentity.TileEntity teNextToJukeBox = (net.minecraft.tileentity.TileEntity) teNextToJukeboxOptional.get();
if (teNextToJukeBox instanceof IInventory) {
IInventory iInventory = (IInventory) teNextToJukeBox;
TileEntityCarrier tileEntityCarrier = (TileEntityCarrier) teNextToJukeBox;
if(iInventory.getSizeInventory() >= 8) {
player.sendMessage(Text.of(TextColors.AQUA, "Found a ", tileEntityCarrier.getType().getName(), " to the ", direction.name().toLowerCase(), " slots: ", iInventory.getSizeInventory()));

TileEntity teNextToJukeBox = teNextToJukeboxOptional.get();
if (teNextToJukeBox instanceof TileEntityInventory) {
TileEntityInventory<?> tileEntityInventory = (TileEntityInventory<?>) teNextToJukeBox;
if(tileEntityInventory.capacity() >= 8) {
player.sendMessage(Text.of(TextColors.AQUA, "Found a ", tileEntityInventory.getName(), " to the ", direction.name().toLowerCase(), " with ", tileEntityInventory.capacity()+" slots."));
musicBoxType = MusicBoxKeys.MusicBoxType.CHEST;
setting.set(MusicBoxKeys.INVENTORY_DIRECTION, Optional.of(direction));
setting.set(MusicBoxKeys.INVENTORY_SLOT, Optional.of(0));
Expand Down Expand Up @@ -161,7 +161,7 @@ public void onInteractWithJukebox(InteractBlockEvent.Secondary event, @First Pla
}

//Check that the item in the players hand is a record and that it has a custom display name
Optional<ItemStack> stackInHand = player.getItemInHand();
Optional<ItemStack> stackInHand = player.getItemInHand(HandTypes.MAIN_HAND);
Location<World> jukeboxLocation = event.getTargetBlock().getLocation().get();
//Only insert the disc if the jukebox doesn't have a disc inside it
TileEntity tileEntity = jukeboxLocation.getTileEntity().get();
Expand All @@ -177,7 +177,7 @@ public void onInteractWithJukebox(InteractBlockEvent.Secondary event, @First Pla
jukeboxLocation.offer(Keys.REPRESENTED_ITEM, stackInHand.get().createSnapshot());

//Remove the disc from the players hand
player.setItemInHand(null);
player.setItemInHand(HandTypes.MAIN_HAND, null);

//Turn off any players at the blocks location first
if (noteBlockPlayers.containsKey(jukeboxLocation)) {
Expand Down Expand Up @@ -248,26 +248,24 @@ private void playNextSong(Location<World> jukeBoxLocation) {

Optional<TileEntity> inventoryTileEntityOptional = jukeBoxLocation.add(invDirection.toVector3d()).getTileEntity();
if(inventoryTileEntityOptional.isPresent()) {
net.minecraft.tileentity.TileEntity teNextToJukeBox = (net.minecraft.tileentity.TileEntity) inventoryTileEntityOptional.get();
if (teNextToJukeBox instanceof IInventory) {
IInventory iInventory = (IInventory) teNextToJukeBox;
TileEntity teNextToJukeBox = inventoryTileEntityOptional.get();
if (teNextToJukeBox instanceof TileEntityCarrier) {
TileEntityInventory<?> tileEntityInventory = (TileEntityInventory<?>) teNextToJukeBox;
//Make sure we don't npe
if(currentPlayingSlot >= iInventory.getSizeInventory()) {
if(currentPlayingSlot >= tileEntityInventory.capacity()) {
currentPlayingSlot = 0;
}

int newSlot;
for (newSlot = currentPlayingSlot; newSlot < iInventory.getSizeInventory(); newSlot++) {
ItemStack stackInSlot = (ItemStack) (Object) iInventory.getStackInSlot(newSlot);
if(stackInSlot != null) {
if (stackInSlot.getItem() == ItemTypes.RECORD_CAT && stackInSlot.get(Keys.DISPLAY_NAME)
for (newSlot = currentPlayingSlot; newSlot < tileEntityInventory.capacity(); newSlot++) {
Optional<ItemStack> stackInSlot = tileEntityInventory.query(new SlotIndex(newSlot)).peek();
if(stackInSlot.isPresent()) {
if (stackInSlot.get().getItem() == ItemTypes.RECORD_CAT && stackInSlot.get().get(Keys.DISPLAY_NAME)
.isPresent()) {
//Check that the song exists for the disc name
String songName = TextSerializers.PLAIN.serialize(stackInSlot.get(Keys.DISPLAY_NAME).get());
//String songName = Texts.legacy().to(itemInHand.get().get(Keys.DISPLAY_NAME).get());
String songName = TextSerializers.PLAIN.serialize(stackInSlot.get().get(Keys.DISPLAY_NAME).get());
Optional<Song> song = MusicBox.getInstance().getSongStore().getSong(songName);
if(song.isPresent()) {

NoteBlockSongPlayer noteBlockSongPlayer = new NoteBlockSongPlayer(song.get());
noteBlockSongPlayer.setNoteBlockLocation(jukeBoxLocation);
noteBlockSongPlayer.setAreaMusic(true);
Expand All @@ -282,14 +280,13 @@ private void playNextSong(Location<World> jukeBoxLocation) {
((Player) entity).sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.AQUA, "Now Playing: " + songName));
}


currentPlayingSlot = ++newSlot;
break;
}
}
} else {
//Failed to find a new song, reset to 0
if(newSlot == iInventory.getSizeInventory() - 1) {
//Failed to find a new song, reset to slot 0
if(newSlot == tileEntityInventory.capacity() - 1) {
currentPlayingSlot = 0;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/uk/co/haxyshideout/musicbox/store/SongStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import com.google.common.collect.Maps;
import com.xxmicloxx.NoteBlockAPI.decoders.nbs.NBSDecoder;
import com.xxmicloxx.NoteBlockAPI.decoders.nbs.Song;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.service.pagination.PaginationList;
import org.spongepowered.api.service.pagination.PaginationService;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.weighted.LootTable;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.gen.Populator;
import org.spongepowered.api.world.gen.populator.Dungeon;
import uk.co.haxyshideout.musicbox.MusicBox;

import java.io.File;
Expand Down Expand Up @@ -52,6 +58,12 @@ public void loadSongs() {
}
buildPaginatedLists();
}).submit(musicBox);

registerDungeonLoot();
}

private void registerDungeonLoot() {
//TODO
}

private void buildPaginatedLists() {
Expand Down

0 comments on commit c2ecb44

Please sign in to comment.