Removal of items in minecraft spigot plugin - instance

So I'm trying to make a mushroom stew that when you drink it, it gives you back 6 food, but when I tried to remove the stew via remove(Material.MUSHROOM_STEW); I realised it can remove all the stew you have in your inventory when you drink it. May I know how to remove 1 item only when you drink it? For the code just label the food meter going up as // do stuff

you can do something like this. On the PlayerItemConsumeEvent we check what item the player ate.
Then if it's a stew, we cancel the eating event then we give the player 6 extra food levels and remove the item from their inventory.
#EventHandler
public void PlayerItemConsumeEvent(PlayerItemConsumeEvent e) {
if(e.getItem().equals(new ItemStack(Material.MUSHROOM_STEW))) {
// player ate stew
Player player = e.getPlayer();
e.setCancelled(true);
player.setFoodLevel(player.getFoodLevel() + 6);
player.getInventory().getItemInMainHand().setAmount(player.getInventory().getItemInMainHand().getAmount() - 1);
return;
}
// player ate something else than stew
}

Related

Cards Stack swipe - add card in the back after swiping (removing) top card

I use this library: https://github.com/yuyakaido/CardStackView. It's like Tinder-like swiping cards left-right and it's based on RecyclerView.
This library has the following settings: cardStackLayoutManager.setVisibleCount(3) - that means that the number of visible cards at any time is MAXed to 3 cards.
Suppose I have an array with 5 elements, and then I load everything to my adapter.
On the screen, I'll see 3 cards - holding the first three elements from the array.
The Problem is that when I swipe the top card, (deleting it from the stack), then I have two cards left, but I'm expecting a new card appear at the bottom with the 4th element from the array, but it never happens unless I remove the first element from an array and then call: notifyDataSetChanged() which reloads the entire stack, adapter (and it comes up with blinking, flashing, etc.)
All I need is to load the NEXT element from the array in the new card at the bottom.
I figured this out. First, in Adapter, since I'm dealing with only 3 preloaded cards in stock at any time, I do this:
int countCards;
SwipeAdapter(ArrayList<String> userIDArr, Context ctx)
{
this.userIDArr2 = userIDArr;
.......
if (userIDArr2.size() >= 3) {
countCards = 3;
} else {
countCards = userIDArr2.size();
}
.......
this.ctx = ctx;
}
#Override
public int getItemCount() {
return countCards;
}
... that's provided that the total array userIDArr.size() might be more than 3 but getItemCount can only be MAX 3 or less (if the array size is less than 3).
Then, in an Activity that deals with an adapter, when I swipe the card whichever way, I remove index 0 (top card) from the array and then call notifyItemRemoved:
userIDArr.remove(0);
if (userIDArr.size() > 2) {
swipeAdapter.notifyItemRemoved(0);
}
NOTICE that notifyItemRemoved is only called when the array still has 3 or more items left.
Works perfectly smooth.

XCTest : How to hit a button from an array

I have a view and five buttons in an array on a uistackview drawn on a storey board:
However, when I record, it always hit the third button and I cannot randomise which button to hit
Code is:
let app = XCUIApplication()
let oneElement = app.otherElements["First"]
let twoElement = app.otherElements["Two"]
if oneElement.exists
{
oneElement.tap()
}
if twoElement.exists
{
twoElement.tap()
}
TwoElement is the one that taps the button
How can I direct the tap to button 1 or 0?
otherelements is what was provided by the recording. I will sned the debug description in a few hours
otherElements
is what was provided by the recording. I will send the debug description in a few hours
this is what was provided by the recording. I will send the debug description in a few hours

How to setup camera in Unity 3D Horse Racing game

I am building a horse racing game in Unity3D, I have problem with camera:
Currently, my camera only focuses on one fixed horse (ex, horse No 1), so it causes when this horse is far away from others then there will be only one horse appeared on screen, it is not good solution.
Anybody has some ideas on this?
Thanks,
You can think as you were making a movie: just place more cameras on the scene, and activate them one at a time. If you want a camera per horse, you could place a camera directly into the horse's prefab (assuming you have it), so that each newly instantiated horse has one of them. Then, you can write a function that permits the cameras' switch:
var cameras : GameObject[];
function SelectCamera (index : int) {
for (var i : int = 0; i < cameras.length; i++) {
if (i == index){
cameras[i].camera.active = true;
}else{
cameras[i].camera.active = false;
}
}
}

Simple Bukkit Plugin - Opening A Chest

Server owner had asked me to write a simple plugin for the server. All it requires is that if you click on a bookshelf with a written book in hand, it will open a chest. When the person closes that chest, all the books placed in it are destroyed.
I understand the basics of bukkit plugin developing, but the instructions on their website are quite complicated. I understand that I would have to register that the players item in hand is indeed a book with this code:
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.WRITTEN_BOOK) {
// Do other stuff in here
}
However, if it would be easier for the plugin to be a button that is pressed and the book in hand is destroyed, would you please let me know and help me go about those steps.
You could listen to PlayerInteractEvent, check if they right-click a bookshelf, then open a chest GUI... You could do it like this:
#EventHandler //ALWAYS use this before events
public void playerInteract(PlayerInteractEvent e){ //listen for PlayerInteractEvent
if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK){ //make sure the player right-clicked a block
if(e.getClickedBlock().getType().equals(Material.BOOKSHELF)){ //make sure the player right-clicked a bookshelf
Player p = e.getPlayer(); //get the player
if(p.getItemInHand() != null){ //make sure the player has something in their hand
if(p.getItemInHand().getType().equals(Material.WRITTEN_BOOK)){ //check if the player has a written book in their inventory
//Inventory inv = Bukkit.createInventory(null, <size (should be divisible by 9)>, "name"); //create the inventory
//An example of creating the inventory would be:
Inventory inv = Bukkit.createInventory(null, 36, "Disposal");
}
}
}
}
}
That should be all that you need to do. Just make sure that you register events in your onEnable() method using:
this.getServer().getPluginManager().registerEvents(new HandlerClassName(), this);
and make sure that the handler class implements Listener.
Pretty much, what the first code above is doing, is:
Listen for PlayerInteractEvent
Make sure that the player involved with the interact event Right Clicked a block
Make sure that the right-clicked block was a Book Shelf
Make sure that the item in the player's hand is not null
Make sure that the item in the player's hand is a Written Book
Create a new inventory named Disposal with 36 slots, or a 9 x 4 chest.
Then, when the player puts items in the chest, and closes it, the items will be destroyed!
If you would like it to just remove the book when the user right-clicks a chest, simply use:
p.setItemInHand(new ItemStack(Material.AIR));
Instead of this:
Inventory inv = Bukkit.createInventory(null, 36, "Disposal");

Problems in my AS2 Game

Hey guys, I'm trying to make a 2D Platform style game similar to this game below:
http://www.gameshed.com/Puzzle-Games/Blockdude/play.html
I have finished making most of the graphic, and areas, and collision, but our character is still not able to carry things. I'm confused as to what code to use so that my character can carry the blocks. I need help as to how to make our character carry blocks that are in front of him, provided that the blocks that don't have anything on top of it. This has been confusing me for a week now, and any help would be highly appreciated. :D
I fondly remember my first AS2 game. The best approach is probably an object oriented approach, as I will explain.
In AS2, there is a hittest method automatically built into objects. There is a good tutorial on Kirupa here:
http://www.kirupa.com/developer/actionscript/hittest.htm
also
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001314.html
First you'll want to generate your boxes using a Box class. Your class would need to look something like the following:
//Box.as pseudo-code
class Box {
var x_pos:Number;
var y_pos:Number;
var attachedToPlayer:Boolean;
function Box(_x:Number, _y:Number) {
this.x_pos = _x;
this.y_pos = _y;
}
//other code here
}
See this tutorial on how to attach a class to an object in the library:
http://www.articlesbase.com/videos/5min/86620312
To create a new Box, you'd then use something like
box1 = new Box(100,200);
// creates a box at position 100x,200y
However, you'll also want to store the blocks you want to pickup into some sort of array so you can loop through them. See http://www.tech-recipes.com/rx/1383/flash-actionscript-create-an-array-of-objects-from-a-unique-class/
Example:
//somewhere near the top of your main method, or whereever your main game loop is running from - note Box.as would need to be in the same folder
import Box;
//...then, somewhere before your game loop
//create an array to hold the objects
var boxArray:Array = new Array();
//create loop with i as the counter
for (var i=0; i<4; i++)
{
var _x:Number = 100 + i;
var _y:Number = 100 + i;
//create Box object
var box:Box = new Box();
//assign text to the first variable.
//push the object into the array
boxArray.push(box);
}
Similarly, you would need a class for your player, and to create a new Player object at the start of your game, e.g.
var player = new Player(0,0);
You could then run a hittest method for your player against the blocks in your array for the main game loop (i.e. the loop that updates your player's position and other game properties). There are probably more efficient ways of doing this, e.g. only looping for the blocks that are currently on the screen.
Once your array has been created, use a foreach loop to run a hittest against your player in your game's main loop, e.g.
//assuming you have an array called 'boxArray' and player object called 'player'
for(var box in boxArray){
if (player.hittest(box)) {
player.attachObjectMethod(box);
}
}
This is basically pseudo-code for "for every box that we have entered into the array, check if the player is touching the box. If the box is touching, use the box as the argument for a method in the player class (which I have arbitrarily called attachObjectMethod)".
In attachObjectMethod, you could then define some sort of behavior for attaching the box to the player. For example, you could create a get and set method(s) for the x and y position of your boxes inside the box class, along with a boolean called something useful like attachedToPlayer. When attachObjectMethod was called, it would set the box's boolean, e.g. in the Player class
//include Box.as at the top of the file
import Box;
//other methods, e.g. constructor
//somewhere is the Player.as class/file
public function attachObjectMethod (box:Box) {
box.setattachedToPlayer(true);
//you could also update fields on the player, but for now this is all we need
}
Now the attachedToPlayer boolean of the box the player has collided with would be true. Back in our game loop, we would then modify our loop to update the position of the boxes:
//assuming you have an array called 'boxArray' and player object called 'player'
for(var box in boxArray){
if (player.hittest(box)) {
player.attachObjectMethod(box);
}
box.updatePosition(player.get_Xpos, player.get_Ypos);
}
In our Box class, we now need to define 'updatePosition':
//Box.as pseudo-code
class Box {
var x_pos:Number;
var y_pos:Number;
var attachedToPlayer:Boolean;
function Box(box_x:Number, box_y:Number) {
this.x_pos = box_x;
this.y_pos = box_y;
}
public function updatePosition(_x:Number, _y:Number) {
if (this.attachedToPlayer) {
this.x_pos = _x;
this.y_pos = _y;
}
}
//other code here
}
As you can see we can pass the player's position, and update the box's position if the attachedToPlayer boolean has been set. Finally, we add a move method to the box:
public function move() {
if (this.attachedToPlayer) {
this._x = x_pos;
this._y = y_pos;
}
}
Examples of updating position:
http://www.swinburne.edu.au/design/tutorials/P-flash/T-How-to-smoothly-slide-objects-around-in-Flash/ID-17/
Finally, to make it all work we need to call the move method in the game loop:
//assuming you have an array called 'boxArray' and player object called 'player'
for(var box in boxArray){
if (player.hittest(box)) {
player.attachObjectMethod(box);
}
box.updatePosition(player.get_Xpos, player.get_Ypos);
box.move();
}
You have also specified that the blocks should only move with the player if they have nothing on top of them. When you call your attachedToPlayer method, you would also need to run a foreach loop inside the method between the box and the objects that might sit on top of the box. You should now have a fair idea from the above code how to do this.
I appreciate that this is quite a lengthy answer, and I haven't had an opportunity to test all the code (in fact I'm fairly positive I made a mistake somewhere) - don't hesitate to ask questions. My other advice is to understand the concepts thoroughly, and then write your own code one bit at a time.
Good luck!
The way I would do this is to design an individual hit test for each block he will be picking up, then code for the hit test to play a frame within the sprite's timeline of him carrying a block, and to play a frame within the block to be picked up's timeline of the block no longer at rest (disappeared?).
Good Luck if you're confused about what I've said just ask a little more about it and I'll try to help you if I can.