Placing blocks with Forge Mod Loader? - minecraft

How to place a block when the user right clicks with the item?
The only method I can find that would do anything is setBlockMetadataWithUpdate() but the parameters are badly explained in the javadocs.

Have you tried taking a look into the ItemBlock class? Using the onItemUse() in combination with the placeBlockAt() function or functionality?
/**
* Called to actually place the block, after the location is determined
* and all permission checks have been made.
*
* #param stack The item stack that was used to place the block. This can be changed inside the method.
* #param player The player who is placing the block. Can be null if the block is not being placed by a player.
* #param side The side the player (or machine) right-clicked on.
*/
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
{
if (!world.setBlock(x, y, z, field_150939_a, metadata, 3))
{
return false;
}
if (world.getBlock(x, y, z) == field_150939_a)
{
field_150939_a.onBlockPlacedBy(world, x, y, z, player, stack);
field_150939_a.onPostBlockPlaced(world, x, y, z, metadata);
}
return true;
}

Related

Mean or Sum of objective function matters in optimization?

I'm using RcppEnsmallen to estimate quantile regression models. I think I have set up everything right, but problems have been constantly emerging. I've tried two strategies:
Strategy 1: in the objective function, return sum of all errors. The corresponding gradient setup is similar. I tried 0.1 quantile in my example. However, occasionally I can get correct answers. Most of the time, it returns NaN.
Strategy 2: The objective function changed to mean of the sum of all errors. The gradient setup is also use the mean version. However, in this scenario, it returns quite different values each time, far from the correct answers.
I was wondering where the problem might be? Thanks.
#include <RcppEnsmallen.h>
// [[Rcpp::depends(RcppEnsmallen)]]
class QuantileRegressionFunction
{
public:
// Construct the QuantileRegressFunction with the given data.
QuantileRegressionFunction(const arma::mat& X,
const arma::colvec& y
) : X(X),y(y){}
// Define the objective function.
double Evaluate(const arma::mat& beta){
// arma::vec residual1 = y - X * beta;
return arma::sum( (y - X * beta) % ( 0.1 - arma::ones(y.n_rows) % ( (y - X * beta) <0 )) );
}
void Gradient(const arma::mat& beta, arma::mat& gradient)
{
gradient = -X.t() * ( 0.1 - arma::ones(y.n_rows) % ( (y - X * beta) <0 ) ) ;
}
private:
const arma::mat& X;
const arma::vec& y;
};
// [[Rcpp::export]]
arma::mat qr_reg(const arma::mat&X, const arma::vec&y){
QuantileRegressionFunction qrf(X,y);
ens::L_BFGS lbfgs;
lbfgs.MaxIterations()= 100000 ;
lbfgs.MaxLineSearchTrials() = 10000;
//lbfgs.ArmijoConstant() = 1e-10;
arma::mat beta(X.n_cols, 1 , arma::fill::randn);
lbfgs.Optimize(qrf,beta);
return beta;
}

understanding a piece of code with ``boolean`` and ``switch``

i was looking some examples of interactions with the keyboard and stumbled upon this code that i found interesting. But i'm having trouble understanding a certain part of it(it's marked down below).I don't get how all this whole ''boolean'' declaration, ''switch'' and ''CASE'' works, i tried to look in the reference but still. Could someone explain in a simple maner how these work?
float x = 300;
float y = 300;
float speed = 5;
boolean isLeft, isRight, isUp, isDown;
int i = 0;
void keyPressed() {
setMove(keyCode, true);
if (isLeft ){
x -= speed;
}
if(isRight){
x += speed;
}
}
void keyReleased() {
setMove(keyCode, false);
}
boolean setMove(int k, boolean b) {// <<<--- From this part down
switch (k) {
case UP:
return isUp = b;
case DOWN:
return isDown = b;
case LEFT:
return isLeft = b;
case RIGHT:
return isRight = b;
default:
return b; }
}
Questions like these are best answered by the reference:
Works like an if else structure, but switch() is more convenient when you need to select between three or more alternatives. Program controls jumps to the case with the same value as the expression. All remaining statements in the switch are executed unless redirected by a break. Only primitive datatypes which can convert to an integer (byte, char, and int) may be used as the expression parameter. The default is optional.
The rest of the code is setting the corresponding variable to whatever value you passed in as the b parameter, and then returning it.
You should get into the habit of debugging your code. Add print statements to figure out exactly what the code is doing.

Issue with scope of variables in C, why a function prints what it's supposed to print

What exactly is going on in this program here? Why does myFunction print 3 for x?
int myFunction(int);
void main(void) /* local variables: x, result in main */
{
int result, x = 2;
result = myFunction(x);
printf("%i", result); /* prints "3" */
printf("%i", x); /* prints "2" */
}
int myFunction (int x)
{
x = x + 1;
printf("%i\n", x); /* prints "3" */
return x;
}
That's because the parameter is a local variable in the function.
When the function is called, there is space allocated on the stack for the parameter, and the value from the variable x is copied into the parameter.
In the function the parameter x is a local varible, separate from the variable x in the calling code. When x is increased in the function, that only happens to the local copy.
When the function ends, the parameter goes away when the stack frame for the function is removed from the stack.
You should read about the differences between pass by value vs. pass by reference in function variables (see here).
In your case, you are passing x by value to myFunction, the function basically gets the value of x (2) and increments it, but never changes the original value of x from your main function.

Netbeans:get the X and Y co ordinate

I have an image in the jlabel. I want to get the X and Y co-ordinate when ever it is clicked on. I used the following code:
private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
f = jLabel2.getMousePosition();
m = new Point(f).toString();
}
And I got the output:
java.awt.Point[x=165,y=105]
But I don't know how take the x and y separately.
The point class two public fields which are x and y. You can access them through your instance variable.
You do the following. int x1 = m.x; int y1 = m.y.
if you need extra information and some useful methods, read the Java Api.
http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

Why can't you assign and call a block at the same time?

The following code compiles:
^{}();
And this compiles:
void (^x)();
(x = ^{})();
But this doesn't:
(void (^x)() = ^{})();
The error I get is Expected ')'. Is this a bug with llvm or something? It's totally holding me back from pretending Objective-C is JavaScript.
This wouldn't make sense in a C-like language. To see why, let's build the statement from the ground up.
First, we'll use your working declaration for x:
void (^x)();
Now let's initialize it in the same statement:
void (^x)() = ^{};
So far so good - x has been initialized with the correct block. So let's invoke x now. But where will the () go? Naturally, we need to place the () immediately after a block-valued expression. However, in C, declarations are statements, not expressions so
(void (^x)() = ^{})();
doesn't make sense. The only place the () can go is after the ^{}:
void (^x)() = ^{}();
But ^{}() has type void, not type void (^)().
To sum up: you can't declare a block variable and invoke it at the same time. you'll have to either declare and initialize the variable, and then call it
void (^x)() = ^{};
x();
or declare it and then assign and call it
void (^x)();
(x = ^{})();
or just separate all three:
void (^x)();
x = ^{};
x();
As a concluding thought, let's say it was desirable to declare and invoke blocks at the same time. If we decided to allow code like (void (^x)() = ^{})();, then for the sake of consistency, we would have to also allow code such as ++(void x = 4); or (void x = 1) + (void y = 2);. I hope you'll agree that these just look strange in C.
As an analogy, consider:
This compiles:
if (42) { }
And this compiles:
int x;
if (x = 42) { }
But this doesn't:
if (int x = 42) { }